Cron Jobs in Linux?
Cron is a time-based job Scheduler in Linux based Operating Systems, People who set up and maintain software environments use Cron to schedule jobs (commands and scripts) to run periodically at fixed times, dates, or intervals.
It typically automates system maintenance or administration – though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals. Cron is most suitable for scheduling repetitive tasks. Scheduling one-time tasks are often more easily accomplished using the associated at the utility.
here is a simple cron job:
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2&1
There are two main parts:
- The first part is “10 * * * *”. This is where we schedule the timer.
- The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
- “/usr/bin/php”. PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
- “/www/virtual/username/cron.php”. This is just the path to the script.
- “> /dev/null 2>&1”. This part is handling the output of the script. More on this later.
* * * * * [command]Runs Every Minute
0 * * * * [command]Run at minute 0 (Every Hour)
15 * * * * [command]Run at 00:15, 01:15, …. Every Hour
30 2 * * * [command]Run at 02:15 AM Every Day
0 * * * 1 [command]Run on Monday Every Hour
0,10,20 * * * * [command]Run Every Hour at 0,10,20 Minute
*/5 * * * * [command]Run 12 times per hours (Every 5 Minutes)
0 5-10 * * * [command]Run Once every hour between 5:00 AM and 10:00 AM
How to Setup in Ubuntu:
1. Press CTRL+ALT+ T to open a terminal.
2. Open editor and create a shell script that we will use to run using Cron Scheduler.
3. Write a shell script for append Text “Hello World” in a file “file.txt”
4. Save the File
5. Close the file
7. In Terminal Run Command “
crontab -e” for create cron job.
8. Add “
* * * * * sh /home/sagun/file.sh” for execute shell script every minute.
9. Save the Crontab using Ctrl+X and follow the instructions.
10.You can see the current cron Jobs using command “
11. It will create cron which is run the shell script every minute. ! enjoy