How to schedule a recurring job (cron job)

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Before reading and applying the content of this article, please familiarize yourself with Linux crontabs by reading Scheduling Tasks with Cron Jobs or searching the Internet.

Purpose

In some cases, there is a need to schedule recurring jobs in Hipchat Server. Examples include but are not limited to: 

  • Cleanup of old attachments
  • Cleanup of log files
  • Database dumps or backups

Solution

In the steps below, we will be focusing on one scenario as an example. The steps should be edited accordingly to fulfil your needs.

The purpose could be achieved through Linux crontabs. Let's take, as an example, a job that will run every day at midnight and delete attachments older than 90 days.

The command to delete attachments older than 90 days is: 

find /file_store/cumulus/posixdata/f/ -mtime +90 ! -iname *emoticon* -delete

The recurring job would be represented as follows in the root crontab: 

0 0 * * * find /file_store/cumulus/posixdata/f/ -mtime +90 ! -iname *emoticon* -delete

Although this job could be added manually by running crontab -e, it will not persist through upgrades and server reboots. 

The below steps should be run in order for the job to persist in your system: 

  1. Log into the Hipchat Server terminal/command-line interface
  2. Create a new file in /home/admin/startup_scripts called attachments_cleanup_cronjob or similar

    Do not include a file extension in the name

  3. Copy the code below into the file and save it:

    #!/bin/bash
    
    # Check if there's already a job that's finding in the attachments folder
    CHECK=$(sudo dont-blame-hipchat -c "crontab -l | grep 'find /file_store/cumulus/posixdata/f/' | wc -l")
    
    # If there are no jobs, add one
    if [ $CHECK -lt 1 ]; then
        sudo dont-blame-hipchat -c "crontab -l | { cat; echo '0 0 * * * find /file_store/cumulus/posixdata/f/ -mtime +90 ! -iname *emoticon* -delete'; } | crontab -";
    fi
     
  4. Save the script, then make it executable:

    chmod +x /home/admin/startup_scripts/attachments_cleanup_cronjob 
  5. Run the script once:

    /home/admin/startup_scripts/attachments_cleanup_cronjob
  6. Validate the change in the root crontab:

    sudo dont-blame-hipchat -c "crontab -l"

    The following line should be among what the command returns:

    0 0 * * * find /file_store/cumulus/posixdata/f/ -mtime +90 ! -iname *emoticon* -delete

The above implements a daily job that runs at midnight. If the job frequency needs to be increased or decreased, please modify the cron job times inside the script and execute it again.

Last modified on Jan 19, 2018

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.