Our community of experts have been thoroughly vetted for their expertise and industry experience. Experts with Gold status have received one of our highest-level Expert Awards, which recognize experts for their valuable contributions.
Automating work can be done on Linux / Unix by creating what is called a cronjob that is periodically running a program or script (job) on a schedule you specify. Jobs are specified in a crontab (file) and are run by the cron daemon process.
The crontab
The crontab itself is a time table that has entries for jobs and the schedule on which these jobs need to execute. Each user on a Linux/Unix system can have a crontab. If you cannot access your crontab, an administrator (root) can grant you access.
The first part "00 11 * * *" is a specification of the time (or schedule) and the second part "rm -f /var/tmp/*" is the job that will be executed at that time.
Basic commands
crontab -l
(show your crontab)
crontab -r
(remove your crontab - be careful)
crontab -e
(edit your crontab)
How to specify the schedule
Let's take a sample entry:
00 11 2 * *
Meaning:
00 - minute (number 0-59)
11 - hour (number 0-23)
2 - day of month (number 1-31)
* - month (number 1-12 / names allowed as well)
* - day of week (number 0-7 / 0 or 7 for sunday, names allowed as well)
The example would mean that a job would be scheduled at 11:00 (AM) on the day 2 of every month. You can specify multiple schedules either by adding lines or by combining a comma as a separator:
For more details on scheduling use "man 5 crontab" on your system. For more information on tables for driving cron jobs, see this very clear description.
How to specify the job
You can specify the job (program, script or commands) by adding it directly behind the schedule:
00 11 * * * /home/username/cronscript.sh
(a script on your homedirectory)
Note: cron does not know of any environment settings you may have setup in your profile. So if you would have ${ORACLE_HOME} in your PATH and you type sqlplus to start the sqlplus application, this will not work in a script that is started by cron. You either have to specify a full path to each command in your script, set the path first or call your environment script.
A real example
In this example, I want to know the status of the power supply of a banana pi device that I have. The details of the power supply can be shown by looking at a special device (uevent).
The script is echoing the current date/time and adding the output of the cat ... uevent to the pwr.txt file.
The last lines of the pwr.txt file:
Sun Feb 15 23:25:02 CET 2015POWER_SUPPLY_NAME=acPOWER_SUPPLY_MODEL_NAME=acPOWER_SUPPLY_PRESENT=1POWER_SUPPLY_ONLINE=1POWER_SUPPLY_VOLTAGE_NOW=4846000POWER_SUPPLY_CURRENT_NOW=313000
Where I can see that the pwr script ran at 23:25 and that the supply voltage is 4846000mV (4.8V) and the device is using 313000uA (313mA) so about 1.5W.
Some best practices
Redirecting output of your job
If you want to know if your crontab is generating output, redirect std error and std output by adding this at the end of your crontab line:
When you look at the file cronscript.lastrun, look at its timestamp to know the last time it ran. If you get no file, it did not run at all
Temporary disabling
To disable one crontab job, edit your crontab using crontab -e and put a # (hash) in front of the line you want to disable.
To disable the whole crontab, first save your crontab entries using crontab -l > saved_crontab and then remove it using crontab -r If you want to enable it again use crontab saved_crontab
Our community of experts have been thoroughly vetted for their expertise and industry experience. Experts with Gold status have received one of our highest-level Expert Awards, which recognize experts for their valuable contributions.
Comments (1)
Commented:
Very useful for Linux sysadmins and Oracle DBAs.
Voted Good article :)
Thanks for sharing it
Regards,
Yashwant Vishwakarma