Page Nav

HIDE

Grid

GRID_STYLE

Use cron to automate your Linux life

Introduction Cron is an automation tool in Linux that allows you to run scripts and utilities on a schedule. The syntax takes a little...

Image result for Use cron to automate your Linux life

Introduction

Cron is an automation tool in Linux that allows you to run scripts and utilities on a schedule. The syntax takes a little while to master, but once you do, you will realize how easy it is to automate simple administrative tasks with cron.

Steps (4 total)

1

Basic syntax of the crontab file.

Crontab is the file that the cron daemon checks for new cron jobs. Before we start editing the file, let us look at the basic syntax of the crontab file. A valid cron job line has the following structure:
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
Each line has five fields for specifying the day of the week and time of the day that the command will be run. Immediately following these five fields is the full pathname of the command that will be run. An example:
2 12 7 * * uptime
This cron line runs the uptime command each month, on the 7th day of the month (no matter what day of the week it is), at 12:02pm. An '*' indicates each time in the field. Another example:
*/5 * * * * uptime
This runs the uptime command every five minutes. The '*/N' indicates that the command will run every N time / date units in the specific field.
2

Open the crontab file

To edit the crontab file, Press Ctrl+Alt+T to open a new terminal window and type:
export EDITOR=vi; crontab -e
This will open the crontab file in the vi text editor.
3

Edit the crontab file

Navigate to the bottom of the cron file using the arrow keys, the enter the times and dates you want the command to run using the syntax describe above, and the full pathname of the command you want to run.
4

Test the cron daemon

If you want to test to verify if the cron daemon is working, create a cron job that uses the following syntax:
* * * * * command to run > /filepath_for_output
This will run the command every minute and send your output to the filepath you specify.

Conclusion

That's it! Almost any command or utility that can be run at the command line can be automated in a cron job, from administrative messages to cloud storage syncing can be achieved using this simple scheduling system.

No comments