How to schedule a recurring job (cron job)

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

プラットフォームについて: Server および Data Center のみ。この記事は、Server および Data Center プラットフォームのアトラシアン製品にのみ適用されます。

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.

*Fisheye および 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.

目的

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

ソリューション

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.

最終更新日: 2018 年 1 月 19 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.