How to schedule a recurring job (cron job)
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:
- Log into the Hipchat Server terminal/command-line interface
Create a new file in /home/admin/startup_scripts called
attachments_cleanup_cronjob
or similarDo not include a file extension in the name
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
Save the script, then make it executable:
chmod +x /home/admin/startup_scripts/attachments_cleanup_cronjob
Run the script once:
/home/admin/startup_scripts/attachments_cleanup_cronjob
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.