Crontab Explainer
Crontab lets you schedule commands and scripts to run automatically on your Ultra service. Each scheduled task is called a cron job.
The crontab command manages your personal crontab file. The cron service reads this file and runs its entries at their scheduled times.
Understand the Crontab Format
- A cron entry contains five schedule fields followed by the command:
* * * * * command_to_execute
│ │ │ │ │
│ │ │ │ └─ Day of the week
│ │ │ └─── Month
│ │ └───── Day of the month
│ └─────── Hour
└───────── MinuteField | Accepted values |
Minute | 0-59 |
Hour | 0-23 |
Day of the month | 1-31 |
Month | 1-12 |
Day of the week | 0-7, where 0 and 7 represent Sunday |
Command | The command or script to execute |
- For example, the following entry runs a command every day at 4:30 AM:
30 4 * * * command_to_execute- Use crontab.guru to check the meaning of a five-field cron expression.
Use Special Characters
Cron supports the following schedule operators:
- matches every possible value.
,separates multiple values. For example,1,15,30matches minutes 1, 15, and 30.- specifies an inclusive range. For example,
1-5in the day-of-week field represents Monday through Friday. /specifies an interval within a range. For example,/15in the minute field runs every 15 minutes.- Comments must begin with
#on their own lines:
# Run the cleanup script every day at 03:00
0 3 * * * /home/username/scripts/cleanup.sh- An unescaped
%in the command section has a special meaning in cron. Escape a literal percent sign as\%, particularly when using a command such asdateinside a cron entry.
Prepare Commands and Scripts
Cron jobs run with a smaller environment than commands entered in an interactive SSH session. Shell startup files may not be loaded, and commands available in your normal shell may not be found automatically.
- Use the full path to scripts and important commands.
- Use absolute paths for input files, output files, lock files, and logs.
- Add an appropriate shebang, such as
#!/bin/bash, to the beginning of each script. - Ensure the script is executable.
- Create log directories before scheduling the job.
- Test the complete command over SSH before adding it to the crontab.
- Execute
command -v <command>over SSH to locate a command when necessary. - For example, prepare and test a script with the following commands:
mkdir -p /home/username/scripts/logs
chmod +x /home/username/scripts/myscript.sh
/home/username/scripts/myscript.sh- Replace
usernamewith your Ultra service username.
Configure Cron Jobs
- Connect to your Ultra service via SSH.
- Open your personal crontab:
crontab -e- Select
nanoif you are prompted to choose an editor. - Add each cron job on a separate line.
- Press
CTRL+X, thenY, and thenENTERto save and exitnano. - Display the installed entries to confirm that the job was saved:
crontab -l- Before making extensive changes, save a backup of your crontab:
crontab -l > /home/username/crontab-backup.txtAdd Cron Jobs
- The following entry runs
myscript.shevery day at noon and appends standard output and errors to a log:
0 12 * * * /home/username/scripts/myscript.sh >> /home/username/scripts/logs/myscript.log 2>&1- The log directory must exist before the job runs.
- To prevent two copies of the script from running simultaneously, use
flock:
0 12 * * * /usr/bin/flock -n /home/username/scripts/myscript.lock /home/username/scripts/myscript.sh >> /home/username/scripts/logs/myscript.log 2>&1- The
-noption makesflockexit immediately when another copy of the job already holds the lock. - To discard both standard output and errors:
0 2 * * * /home/username/scripts/myscript.sh > /dev/null 2>&1Troubleshoot Cron Jobs
If a job does not run as expected:
- Execute the complete command manually over SSH.
- Execute
date '+%F %T %Z %z'and confirm that the cron expression uses the service's system time. - Execute
crontab -land confirm that the entry was saved. - Confirm that all referenced paths are correct and absolute.
- Confirm that the script is executable.
- Confirm that the log directory exists and is writable.
- Check the job's log for errors.
- Add environment variables such as
PATHorSHELLat the top of the crontab only when the command requires them. - Confirm that an existing
flocklock represents a currently running process before attempting to bypass it. - For a practical scheduled-backup example, see External Backup Script using Rclone Mount.
If you require further assistance, you can open a support ticket here.