I want to run a command every 15min. Here is the command I want to run:
sudo bash -c "sleep 1h; pm-suspend"
I want to run a command every 15min. Here is the command I want to run:
sudo bash -c "sleep 1h; pm-suspend"
I am unable to comment due to insufficient reputation however @Edwin Hernandez crontab example of */15, *, *, *, * sudo bash -c "sleep 1h; pm-suspend"
is incorrect as there should not be commas between the fields (each of the stars are a separate field). @Mitch♦ has the correct format.
cron
's format is:
m h dom mon dow command
m is the minute, h is the hour, dom is day of month, mon is the month, and dow is day of week.
In your comment you ask a supplementary question about running every day and every week.
14 04 * * * command
is every day at 04:14am
39 06 * * 3 command
is every Wednesday at 6:39am (0 = Sun, 1 = Mon, etc...)
39 06 * * wed command
as previous line
Caution #1: If you use * * * * wed command
then it will run every minute of every hour every Wednesday. If you only want it to run once a day then you need to specify the minutes and hours of the day you want it to run. I've been bitten by this on more than one occasion. It can be very frustrating.
Caution #2: If the command you want to run is the pm-suspend
command then I would suggest that you look at all crontab files for all users to find a suitable time after they have finished processing any commands you feel are important. e.g. if you are running a backup starting at 05:44 and then set your pm-suspend
to run at 05:50 there is a possibility that the backup will not have finished before suspending which is probably not a helpful state of affairs.
The reason that the "random" times are used in the example is that crontabs often set off their commands at these "random" times to spread the load so that a lot of processes are not all starting at once
It should be noted that any output of cron is emailed to the user if email is set up. You will not see any output on the screen/terminal.
An aside
If you ever need to watch something change over a shorter time frame (the default is 2 secs) then watch
is a useful command. Finding this command was a godsend!
Other reading
On SO How can I make a bash command run periodically? has lots of links in that might help explain things in alternative ways.
Or if you can cope with the somewhat meaty man
pages:
man 8 cron
man 1 crontab
man 5 crontab
You should use 'cron'. Official instructions here. There is a complimentary anacron
daemon too, that allows to create rules like 5 minutes after connecting to Internet
you could try it using while
loop, save the following code in a file and run it as sudo bash <filename>
. you have to run it once, it will run in terminal as long as you dont cancel it using Ctrl+C
#!/bin/bash
sudo bash -c "sleep 1h; pm-suspend"
sudo crontab -e
instead of having sudo in the cron job command. As the other will need to wait for the password input. But even better is to usecontab -e -u <some-user-name>
if it is an option. – Dan Dec 14 '14 at 19:54sudo
in crontab. Where should the password come from? – Volker Siegel Dec 14 '14 at 21:18