2

I want to run a command every 15min. Here is the command I want to run:

sudo bash -c "sleep 1h; pm-suspend"
αғsнιη
  • 35,660

5 Answers5

6

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

2

A step by step guide would be like:

  1. Editing your crontab: type in a terminal:

    $ crontab -e
    
  2. Add the following line to it.

    */15 * * * * sudo bash -c "sleep 1h; pm-suspend"
    
  3. Save and quit: type :wq and then press enter if you are on vi.

αғsнιη
  • 35,660
Evin1_
  • 249
1

One way is to run a cron command every 15 minutes. Like this, where each minute is specified:

0,15,30,45 * * * * /path/to/command
Mitch
  • 107,631
0

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

0

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"
Alex Jones
  • 7,982
  • 9
  • 55
  • 94