Expand my Community achievements bar.

SOLVED

Restart AEM daily midnight

Avatar

Level 4

Hi,

I am looking for an script to re start AEM instance, is there a way to do it in the AEM config ?

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

 

If you are using AEM on-premise and AEM has been installed as a system service, you can easily use a "restart" command from the command line, in this scenario, you will have to create a bash script that will invoke the "aem service restart" and this will be scheduled at midnight, you can use "cron" for example to achieve the schedule(1), here you can find the details about this scenario: https://github.com/ksurendra/aem-as-a-service 

If you are using AEM on-premise but AEM has not been installed as a system service, you will have to manually stop the AEM instance, verify the process is stopped, and start again the instance, this again will be a script that will be scheduled. You can find more info here about this scenario: https://aem-development.com/knowledge-base/how-to-restart-aem-instance/ 

 

If you are using AEMaaCS, I think there is no need to really bounce an instance due on each deployment this will be recreated, please read about how AEMaaCS works: https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/overview/architectu... which can help to clarify your doubts.

Something worth mentioning as well is that you can restart the system information that depending on your requirements can be useful: please check this article as well: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/difference-between-aem-res... 

 

Hope this helps

 

(1). https://stackoverflow.com/questions/37574458/how-to-run-a-bash-script-via-cron



Esteban Bustamante

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

Hi,

 

If you are using AEM on-premise and AEM has been installed as a system service, you can easily use a "restart" command from the command line, in this scenario, you will have to create a bash script that will invoke the "aem service restart" and this will be scheduled at midnight, you can use "cron" for example to achieve the schedule(1), here you can find the details about this scenario: https://github.com/ksurendra/aem-as-a-service 

If you are using AEM on-premise but AEM has not been installed as a system service, you will have to manually stop the AEM instance, verify the process is stopped, and start again the instance, this again will be a script that will be scheduled. You can find more info here about this scenario: https://aem-development.com/knowledge-base/how-to-restart-aem-instance/ 

 

If you are using AEMaaCS, I think there is no need to really bounce an instance due on each deployment this will be recreated, please read about how AEMaaCS works: https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/overview/architectu... which can help to clarify your doubts.

Something worth mentioning as well is that you can restart the system information that depending on your requirements can be useful: please check this article as well: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/difference-between-aem-res... 

 

Hope this helps

 

(1). https://stackoverflow.com/questions/37574458/how-to-run-a-bash-script-via-cron



Esteban Bustamante

Avatar

Employee Advisor

Hi @ashish_mishra1!

AEM itself does not have a built-in restart scheduling. Depending on your setup (on-premise or AMS with AEM 6.5 vs. AEM Cloud Service), there might be different options to achieve this.What "flavor" of AEM are you running and where is it hosted?

 

  • On AEM CS, there is no way to restart an instance as this is not required and also not how a Software as a Service (SaaS) setup works.
  • On AMS, you probably still don't want to do this as a downtime during restart might cause monitoring alerts (causing an on-call engineer to be notified at midnight) and influence contractual SLAs. Please discuss the topic with your Custom Success Engineer (CSE) at AMS or your Technical Account Manager (TAM) to find a better solution.
  • For self-hosted on-premise setups of AEM, it is certainly possible to realize scheduled restarts of AEM. However, automatically restarting services (not just AEM) comes with a couple of challenges. What do you do if the service does not come up properly during restart? How do you ensure proper functionality after restart? Will your monitoring be triggered by the restart because it will cause a downtime? Does this alert your OPS team at midnight? Does the restart conflict with regular maintenance tasks that are usually also run during low traffic hours (most probably at night)? Are there scheduled jobs inside of Sling/AEM as part of your application that might be disturbed/aborted due to the restart?

Overall, there are many reasons not to implement a daily restart of AEM and various challenges from an operational point of view if still trying to do it.

 

That leads to an important question: Why do you want to restart AEM regularly? What is your motivation?

 

If you are experiencing issues with AEM, such as decreasing performance or increasing issues/errors that can be solved by a restart of the instance, the better option would be to start a root cause analysis and work on the cause, not the symptoms.

 

Avatar

Community Advisor

@ashish_mishra1 ,

 

In AEM, there isn't a built-in configuration specifically designed for scheduling automatic restarts. However, you can use a python script. Same python script will help you to start and restart the instance.

 

 

AEM_PATH = "/path/to/aem"
AEM_JAR = "aem-author.jar"

def start_aem():
    try:
        # Start AEM
        subprocess.run(["java", "-jar", f"{AEM_PATH}/{AEM_JAR}"], check=True)
        print("AEM started successfully.")

    except subprocess.CalledProcessError as e:
        print(f"Error: {e}")
        print("AEM start failed.")

def stop_aem():
    try:
        # Stop AEM
        subprocess.run([f"{AEM_PATH}/crx-quickstart/bin/stop"], check=True)
        print("AEM stopped successfully.")

    except subprocess.CalledProcessError as e:
        print(f"Error: {e}")
        print("AEM stop failed.")

def job():
    # Start AEM
    start_aem()

    # Wait for a specified duration (in seconds)
    duration = 60  # Adjust this to the desired duration
    print(f"Waiting for {duration} seconds...")
    time.sleep(duration)

    # Stop AEM
    stop_aem()

 

 

The schedule.every().day.at("00:00").do(job) line schedules the job to run every day at midnight.

 

# Schedule the job to run every day at a specific time (adjust as needed)
schedule.every().day.at("00:00").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

 

If you dont want to use python then you can use a scheduling tool like cron on Linux/macOS or Task Scheduler on Windows to run shell script at the desired interval.

Avatar

Employee Advisor

Please be aware of what I have outlined in my post:

  • This does not apply to AEM CS.
  • AMS and your CSE won't appreciate it if you do this for a setup hosted with them.
  • Even for on-premise setups, you might want to double check with your operations team on this.

That being said, please also be aware that the provided python script is pretty basic and I would not recommend to use it in a scheduled, automatic mode for any relevant (meaning: productive) environment. It is lacking several checks (e. g. if AEM is already running on "start_aem"; a general status check after start up; a verification that AEM was actually shut down in "stop_aem"). There is a whole lot of exceptional and corner cases where solely relying on return codes from sub-processes is insufficient and results in an undefined state of your application. Also relying on fixed sleep timers to wait for AEM to reach a certain state (being it shut down or start up) is highly unreliable. This might cause various issues, up to repository corruption and a state where AEM is not running properly after the script is finished.

 

Finally, to me as a person not being familiar with Python, it seems that "job()" does have a logical flaw: It does trigger an AEM start up, waits for 60 seconds (hoping that AEM is fast enough) and shuts it down as a last step. After this routine, AEM will be in a shut down state (and your OPS team not happy). The start- and stop-methods should most probably be switched here: First shutting AEM down, waiting (and hoping) that AEM actually shuts down in the defined time frame and afterwards starting it up again. But as mentioned above: there are several sanity checks missing to make this more robust.

 

And re-emphasizing on my initial post: What is your motivation for scheduled restarts in the first place?

Avatar

Community Advisor

You are right @markus_bulla_adobe , It is a basic python script and one can use it for personal use only for local instances.