Cron: Difference between revisions
(add a brief overview of what a crontab file is) |
(add crontab options) |
||
Line 32: | Line 32: | ||
</pre> | </pre> | ||
=== Crontab Options === | === Crontab Options === | ||
To install, update, or edit a job in crontab, use the -e option | |||
<pre> | |||
$ crontab -e | |||
</pre> | |||
To list crontab entires, use the -l option | |||
<pre> | |||
$ crontab -l | |||
</pre> | |||
To remove a job from crontab, use the -r option | |||
<pre> | |||
$ crontab -r | |||
</pre> | |||
To remove confirm removing a job from crontab, use the -i option | |||
<pre> | |||
$ crontab -i -r | |||
</pre> | |||
To add SELINUX security to a crontab file, | |||
<pre> | |||
$ crontab -s | |||
</pre> | |||
To edit another user's crontab file, use the user -u option and specify the username | |||
<pre> | |||
$ crontab -u username -e | |||
</pre> | |||
To list other user crontab entries | |||
<pre> | |||
$ crontab -u username -l | |||
</pre> | |||
== Examples == | == Examples == |
Revision as of 18:02, 10 March 2021
Cron
What Is Cron?
Cron is a unix time based job scheduler tool used to schedule command execution at a specified time interval.
Overview
A crontab file houses instructions to the cron daemon of the basic form: "run this command at this time on this date". Each user can have their own crontab file.
Crontab Syntax
crontab [ -u user ] file crontab [ -u user ] { -l | -r | -e } crontab [ -u user ] [ -i ] { -e | -l | -r } crontab [ -u user ] [ -l | -r | -e ] [-i] [-s]
Cron Expression Table
Cron actions are driven by a crontab file, also known as a "cron table". The user's crontab file is a configuration file that specifies shell commands to run at a given schedule.
Each line of a crontab file represents a job, and looks like this:
# ┌───────────── minute (0 - 59) # │ ┌───────────── hour (0 - 23) # │ │ ┌───────────── day of the month (1 - 31) # │ │ │ ┌───────────── month (1 - 12) # │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday; # │ │ │ │ │ 7 is also Sunday on some systems) # │ │ │ │ │ # │ │ │ │ │ # * * * * * <command to execute>
Crontab Options
To install, update, or edit a job in crontab, use the -e option
$ crontab -e
To list crontab entires, use the -l option
$ crontab -l
To remove a job from crontab, use the -r option
$ crontab -r
To remove confirm removing a job from crontab, use the -i option
$ crontab -i -r
To add SELINUX security to a crontab file,
$ crontab -s
To edit another user's crontab file, use the user -u option and specify the username
$ crontab -u username -e
To list other user crontab entries
$ crontab -u username -l
Examples
Using Cron to Initiate An Sbatch To Run A Bash Script
Bash Script
#!/bin/bash # resources #SBATCH cpus-per-task=1 #SBATCH mem-per-cpu=4G #SBATCH array=-1%1 #SBATCH partition=express # job name, error and output files #SBATCH --job-name=cron-job #SBATCH error=err_%j_4a.log #SBATCH output=out_%j_%4a.log #SBATCH ntasks=1 # email address to request notifications when the job is complete or if it fails #SBATCH --mail-type=FAIL #SBATCH --mail-user=$blazerid@uab.edu # store text file in the home directory echo "hi" > $HOME/test.txt
Crontab File
#!/usr/bin/env # run generate_data.py # git add, commit, and push MAILTO=blazerid@uab.edu #git add data.csv #git commit -m "update data" #git push # submit script to queuing system # runs cronjob.sh every minute of every day * * * * * /cm/shared/apps/slurm/18.08.9/bin/sbatch /data/user/user_id/project_dir/cronjob.sh
The first line in the cron configuration file (crontab file) is the shebang. When this file is executed, if the file content beings with #!, the kernel executes the file specified on the #! line and passes the original file as an argument. Essentially, the shebang tells the terminal which program to use to run your scripts if your script is an executable file.
#!/usr/bin/env