This site is now 100% read-only, and retired.

Running scripts after a reboot for non-root users

Posted by Steve on Wed 15 Mar 2006 at 08:04

In the past we've discussed adding new init scripts to a Debian GNU/Linux system so that programs or daemons can be started when a machine is rebooted. That works very well if you're root, but for non-root users it isn't an option.

Adding init scripts is simple enough, but to write to /etc/init.d you do need to be root.

Instead of using the init scripts like this you can use the cron command scheduler which we've previously introduced.

When we covered it before we only talked about running scripts at particular intervals, such as:

  • Once an hour.
  • Once a day.
  • Once a week.

There is an alternative way of adding entries to your crontab file though, using shortcuts. The shortcuts allow you to make entries that will match at particular times.

The cron shortcuts each begin with the @ character.

As an example the following will run the script /home/steve/bin/foo after the system has been rebooted regardless of the time of day:

@reboot /home/steve/bin/foo

To add an entry to your crontab file (which is the file that the cron daemon reads for per-user scheduled events) you simply execute:

crontab -e

This will open your crontab file in whichever editor you have setup. To choose a particular editor, such as emacs you could run:

EDITOR=/usr/bin/emacs crontab -e

Once you've saved your file you can view your new entrie(s) with "crontab -l":

skx@itchy:~$ crontab -l
# m h  dom mon dow   command
@reboot /home/steve/bin/foo

There are several shortcuts you can use in addition to @reboot. The full list is:

  • @reboot
  • @yearly
  • @annually
  • @monthly
  • @weekly
  • @daily
  • @midnight
  • @hourly

Each of these shortcuts, other than reboot, has an equivalent mapping to the "normal" crontab format. (e.g. @monthly is identical to (0 0 1 * *")

 

 


fetchmail
Posted by bdf (134.184.xx.xx) on Wed 15 Mar 2006 at 09:07
It's an interesting topic wether regular users should have daemons and how they should be started. I think fetchmail is a good example: you run it as a user to pull mail from a remote account to the local mail system.

The first option is to run it as a regular program from cron, every 5 minutes or so. This is what I do now, but in this setup fetchmail does not know about other runs: if the previous run didn't finish before the next one is started, I get an error like "fetchmail: another foreground fetchmail is running at ...". It seems like fetchmail could be better informed if it were running as a daemon (the second option), but there is so much overhead to this: who is going to start it at bootup? do we need to stop it when switching runlevels? can the user easily manage the daemon through some init.d/fetchmail start|stop|restart|...? where does the daemon send log output? if the fetchmail daemon dies because of some unexpected error, how will I know, or will it be restarted? (who isn't paranoid about missing mail?)

Because of all of this concerns, solutions like your @reboot or this fetchmail-init haven't been able to convince me that it is manageable for users to have daemons.

[ Parent ]

Re: Running scripts after a reboot for non-root users
Posted by etptupaf (158.227.xx.xx) on Wed 15 Mar 2006 at 09:41
Thank you for this tip, Steve.

I usually run a script when I arrive to my office which runs fetchmail once and changes in the server a .forward file, so that mail is redirected to my machine (thus, I do not need to run fetchmail any more during the day). When I leave, I run another script that renames in the server that .forward file, so mail accumulates until next day in the server.

Now I see a nice way to run the first script automatically, via the @reboot in a cron file. Question: is there a similar way to run a script on closing down the machine for a non root user?

[ Parent ]

Re: Running scripts after a reboot for non-root users
Posted by Anonymous (129.20.xx.xx) on Thu 16 Mar 2006 at 07:01
If a non-root user is closing down the machine, this is much easier: call the shutdown function from your script (and call your script for shuting down :-).

[ Parent ]

Re: Running scripts after a reboot for non-root users
Posted by Anonymous (82.130.xx.xx) on Tue 28 Mar 2006 at 14:30
but there is a big problem with @reboot in crontab: sometime @reboot it is run to early. I.e., as you can see in /etc/init.d/rc3.S cron is executed with priority 89, and apache with priority 91, so you cannot run the classic wget -O - http://localhost/cleanup.php .

So check the dependencies carefully, before to use @reboot.

[ Parent ]

Re: Running scripts after a reboot for non-root users
Posted by Anonymous (24.201.xx.xx) on Tue 12 May 2009 at 14:51
The:
*/5 * * * * /command
will run the command on interval of 5 min.

[ Parent ]