Cron is a time-based scheduler with which we can schedule to execute specific commands/script in background.Crontab (cron table) is a text file contains user’s cron jobs,and there is a daemon named cron monitor and execute cron jobs in background.
For more detail information,you can
To open crontab
If you need to run commands with administrative privileges
sudo crontab -e
Open crontab with your favorite editor.(By default it’s vi or will let you choose one from list)
export EDITOR=gedit crontab -e
You can append “export EDITOR=gedit” to your ~/.bashrc file if necessary.
To list crontab content
To remove all your cron jobs
* * * * * command to be executed ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └───── day of week (0 - 6) (0 is Sunday, or use names) │ │ │ └────────── month (1 - 12) │ │ └─────────────── day of month (1 - 31) │ └──────────────────── hour (0 - 23) └───────────────────────── min (0 - 59)
- Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-separated values can be used to run a command continuously
- You can use repeat pattern like */5 for every 5 minutes
1.Run mycommand at 5:09am on January 1st plus every Monday in January
09 05 1 1 1 mycommand
2.Run mycommand at 05 and 35 past the hours of 2:00am and 8:00am on the 1st through the 28th of every January and July.
05,35 02,08 1-28 1,7 * mycommand
3.Run mycommand every 5 minutes
*/5 * * * * mycommand
Predefined scheduling definitions
||Run once a year, midnight, Jan. 1st||
||Run once a month, midnight, first of month||
||Run once a week, midnight on Sunday||
||Run once a day, midnight||
||Run once an hour, beginning of hour||
||Run at startup|
Run GUI apps via Cron
To do this just add a “env DISPLAY=:0” before the command you want to schedule,eg:
00 06 * * * env DISPLAY=:0 gui_appname
You can verify your DISPLAY use below command in command line
By default,every user can create their own cron jobs in Ubuntu
If the /etc/cron.allow file exists, then users must be listed in it in order to be allowed to run the crontab command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does, then users must not be listed in the /etc/cron.deny file in order to run crontab.
In the case where neither file exists, the default on current Ubuntu (and Debian, but not some other Linux and UNIX systems) is to allow all users to run jobs with crontab.