Hello all,
I want to run a script every 5 minutes. How to accomplish this task?
Thanks in advance
Mrudula
Hello all,
I want to run a script every 5 minutes. How to accomplish this task?
Thanks in advance
Mrudula
cron is your friend.
Cron from the name Chronos (the god of time)
The cron daemon is a long-running process that executes commands at specific dates and times. You can use this to schedule activities, either as one-time events or as recurring tasks
For commands that need to be executed repeatedly (e.g., hourly, daily, or weekly), you can use the crontab command.
The crontab command creates a crontab file containing commands and instructions for the cron daemon to execute. You can use the crontab command with the following options:
crontab filename installs a crontab from the file
crontab -e Edit your crontab file, or create one if it doesn't already exist.
crontab -l Display your crontab file.
A typical crontab will have system maintenance type tasks but any task can be put into cron.
Each entry in a crontab file consists of six fields, specifying in the following order:
minute(s) hour(s) day(s) month(s) weekday(s) command(s)
10 3 * * 0,4 /etc/cron.d/logchecker
10 3 * * 0 /usr/lib/newsyslog
15 3 * * 0 /usr/lib/fs/nfs/nfsfind
Thanks for the wonderful explanation Rob !! Now lemme try to work it out ...
To choice if you want to execute this script for everybody or for a specific user, that don't need to be the logged user like crontab -e, you can edit the file /etc/crontab. This file is similar similar, but there you have more one parameter, that is the user.
I just tried a simple thing - to display time on the terminal every 5 minutes .. this is my crontab entry for it - but it doesnt seem to work ... y is that?
pwd
/usr/lib
contab -e
no crontab for root - using an empty one
crontab: installing new crontab
crontab -l
0,5,10,15,20 * * * * (echo -n ' '; date; echo " ") > /dev/console
I guess yes...
Maybe no permission. If u syntax is wrong, its make a error in "crontab: installing new crontab".
To execute you script every 5 Min, you can use:
*/5 * * * * command
If I use,
*/5 * * * * /home/shahnaz/abc.sh >> /home/shahnaz/abc.log
I get an error saying like
crontab: error on previous line; unexpected character found in line.
Can you say why it was thrown such error?
The "/5" syntax is for some versions if Linux.
What Operating System are you using?
Try:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/shahnaz/abc.sh >> /home/shahnaz/abc.log 2>&1
I created a file called mycron in /home/mrudula with the following :
0, 5, 10, 15 * * * * (echo "Hello mycron") > /dev/console
then I tried to add this file to the crontab entries by the follwing command
crontab mycron
This is the msg I got -
"mycron":1: bad minute
errors in crontab file, can't install
If I do, crontab -l, mycron contents r echoed on the shell
why did i get such an error before? how can i get it executed correctky so that every 5 minutes i get that msg?
pls help
Thanks
Mrudula
See lstorm2003 example.
No spaces in the minutes field.
(BTW. The name of a crontab file is usually the name of the account owning that crontab).
0,5,10,15
When you edit your crontab file (crontab -e), all output is mailed to the user, unless a different email address is specified as the value of the MAILTO in your crontab.
crontab output is not typically something that would out put to a terminal.
I use Fedora
-----Post Update-----
Hey guys ... its working ... thank u