Wikimedia Cloud documentationThis is a documentation page for Wikimedia Cloud. If you can improve it, please do that. You can also request help on #wikimedia-cloud connect. For all documentation pages, please see the List of all documentation pages for Wikimedia Cloud. If you are seeking a help with a specific project, please contact the project owners.
Cron is a pre-installed software on every instance that allows you to run a specific job at a specific time of the day. Crons are especially useful if you need to run a script periodically but won't be physically available to run the script yourself. To edit your crontab, run the command:
The crontab syntax is the same for any type of Cloud VPS instance you have. Here is an example:
# m h dom mon dow command */5 * * * * sh /home/johndoe/something.sh > /dev/null
There are five parameters to the specific time/date of the week you would like this cronjob to run. A (badly) predefined table is available for each crontab to guide you in using the proper syntax for running your cronjob.
This is the list (based on the above table) and the explanation:
- m: The first parameter is the minute in the hour to run the job. Notice the */5 syntax above, which represents every 5 minutes.
- h: The second parameter is the hour of the day to run the job. The asterisk is to make it run in every hour.
- dom: The third parameter is the day of the month to run the job. The asterisk is to make it run every single day.
- mon: The fourth parameter is the month of the year to run the job. The asterisk is to make it run every month.
- dow: The fifth parameter is the day of the week to run the job. 0 represents Sunday, 1 represents Monday and so on. The asterisk is to make it run every day in the week.
- command: The sixth parameter is the main command you want to run. You can put the whole string of commands in here (separated by the "&&" symbol), though it is recommended to put everything inside a shell/bash script and call the script from the crontab (to make things much easier).
A VERY important note: PLEASE add the "> /dev/null" to the end of the command (you can change /dev/null to another file in your instance for logging purposes). There have been complaints about people who don't do this and spamming the inbox of the ops team. Alternatively set
MAILTO=<your email address>
As you may not host anything in your home directory in the bots project you can use the following:
You can later reference it with $HOME.
Continuous or infinitely long running programs
Such programs (like e.g. irc bots) need essentially to be started once and should then run without further action. However it might happen that it stops running because of an error or even a server reboot. In order to circumvent such issues, the program should be verified to run correct regularly. This can be done by a cronjob, but cron will not check whether the process is already present or not. The solution to this is pgrep and a cronjob like this:
COMMAND="<command>" * * * * * pgrep -u $LOGNAME -fx -- $COMMAND > /dev/null || $COMMAND &> /dev/null
or, for scripts with #! where the string to grep for is different from the invoking command:
* * * * * pgrep -u $LOGNAME -x script.ext > /dev/null || /path/to/script.ext &>> /path/to/logfile
This entry runs every minute (can be reduced to e.g. once per day or else) and executes the <command> only if not already present.
In order to check the currently running processes a list can be retrieved with:
pgrep -u $LOGNAME -lf
There are always more help available on the Internet to guide you through making your own crontab.
If you prefer live help, always ask around.