Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Run programs at startup via crontab UNIX
There is an easy way to start a program during system boot. Just put this in your crontab:
  @reboot /path/to/my/program
The command will be executed on every (re)boot. Crontab can be modified by running
  crontab -e
More information about crontab options is available in the man page:
  man 5 crontab
[robg adds: I haven't tested this one...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[64,532 views]  

Run programs at startup via crontab | 20 comments | Create New Account
Click here to return to the 'Run programs at startup via crontab' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Look at CRONTAB(5)
Authored by: jecwobble on Nov 05, '04 12:21:09PM

You can get a complete list of shortcut cron settings by typing "man 5 crontab"



[ Reply to This | # ]
Run programs at startup via crontab
Authored by: Gigacorpse on Nov 05, '04 12:47:42PM

This does work. Mac OS X uses Vixie Cron, the same that NetBSD uses. It also supports several other "special" settings.

cron 5 crontab



[ Reply to This | # ]
Run programs at startup via crontab
Authored by: aixccapt99 on Nov 05, '04 02:20:18PM

man 5 crontab



[ Reply to This | # ]
Run programs at startup via crontab
Authored by: Gigacorpse on Nov 08, '04 01:54:47PM

Thanks, I mistyped!



[ Reply to This | # ]
Cronnix: cron gui
Authored by: lullabud on Nov 05, '04 01:00:17PM
Cronnix is a really nice interface to OS X's crontab and let's you modify the system crontab as well as any user crontab.

[ Reply to This | # ]
well...
Authored by: nick on Nov 05, '04 01:20:19PM

if you rely on other processes running when you start your programm or your programm running before other programms start you should use the /Library/StartupItems-folder and adapt the StartupParameters.plist-file accordingly. just look for other startupitems and how they're done. p.e.:

mkdir /Library/StartupItems/MyProg
pico /Library/StartupItems/MyProg/MyProg and enter s/t like this:
-----------------------------------------------
#!/bin/sh
## My Prog is started and stopped here:
StartService ()
{
/COMMAND/TO/START/MyProg
}

StopService ()
{
/COMMAND/TO/STOP/MyProg
}
-------------------------------------------
chmod 755 /Library/StartupItems/MyProg/MyProg

if you wanna define some startup-order do:

pico /Library/StartupItems/MyProg/StartupParameters.plit and enter s/t like this (depends on your needs):
-------------------------------------------
{
Description = "My cool Prog";
Provides = ("MyProg");
Requires = ("DirectoryServices");
Uses = ("Disks", "NFS", "Network Time");
OrderPreference = "None";
}
------------------------------------------

shure the other thing works, too. but this is the standart-way to do this on osx and thats the way you get a message in the startup-box when your booting.

n.



[ Reply to This | # ]
What benefit?
Authored by: jhaffner on Nov 05, '04 01:20:32PM

Just out of curiosity, what's the benefit of using this hint over putting things into your startup items under the Accounts pref pane?

John

---
Don't worry, it's out of control.



[ Reply to This | # ]
What benefit?
Authored by: simonpie on Nov 05, '04 01:41:00PM

Apps that you put in your startup field in account preference are launch at login not boot time.



[ Reply to This | # ]
What benefit?
Authored by: rhesuspieces00 on Nov 05, '04 03:14:28PM

well, for most users, im not sure if there is a huge benefit. i know my school's sysadmins use crontabs to do some pretty useful things, but they would mostly not be relevant to single user machines. but, if say you wanted specific scripts to run for a user enrolled in a particular class whenever he logs in, or if he logs in on a particular day of the week...whatever, you can do things like that...prompt the user to change his password every 6 months, schedule backups, etc, etc.

powerful, if you care to learn how to use it.



[ Reply to This | # ]
What benefit?
Authored by: Ksenia on Nov 05, '04 07:18:25PM

I found it to be more easy to startup command line programs. No need to create whole Startup item for that... just one line in the crontab.



[ Reply to This | # ]
What benefit?
Authored by: jpbjpbjpbjpb on Nov 05, '04 08:37:33PM

Except that Apple may or may not step on your modified crontab file in a system update. If you use a StartupItem (and put it in /Library/StartupItems where non-Apple ones belong), you don't have to worry about a system update silently causing your bootup tasks to stop working.



[ Reply to This | # ]
What benefit?
Authored by: Nik_Doof on Nov 06, '04 07:13:56AM

Well, if you add a user crontab and not edit the system crontab (which u shouldnt to anyway) then it will never happen.



[ Reply to This | # ]
What benefit?
Authored by: maartensneep on Nov 06, '04 05:36:56PM

Some commands have to be issued by a privileged user, so you couldn't put them in a user crontab. An example of this situation is to have (some) maintenance script run at start-up (like the periodic daily or periodic weekly scripts I pointed at in another comment). Since quite a few computers are off or asleep during the night, most computers have their periodic scripts run at irregular intervals.

I'd love to have further extensions to this, something like: @reboot,weekly (which would mean: at reboot, but at most once a week. This would add an administrative burden through). Alternatively: @reboot,monday (at reboot, but on mondays only).

Maarten

Maarten

[ Reply to This | # ]
Run programs at startup via crontab
Authored by: maartensneep on Nov 05, '04 03:16:45PM
Normally (that is when you leave your computer on all the time) several maintenance scripts are run in the early morning hours. Since most people will leave their machines asleep during the night, or even turned off altogether, these maintenance scripts are never run. Depending on your reboot habits, you could add

@reboot periodic daily
to the master crontab in /etc/crontab. If you reboot infrequently, you could add @reboot periodic weekly and @reboot periodic monthly as well. weekly takes some time, but I believe the lot is run in the background anyway, so that isn't too bad (weekly updates refresh the locate database for quick command-line searches). Maarten

[ Reply to This | # ]
Run programs at startup via crontab
Authored by: syko on Nov 05, '04 09:20:13PM

What's the main differences between crontab -e

and modifying /etc/crontab?

i know crontab -e can modify a users crontab as well as a system one, but /etc/crontab seems separate from the command crontab -e

which one to use and modify?



[ Reply to This | # ]
Run programs at startup via crontab
Authored by: guybrush on Nov 06, '04 02:58:13PM

There is no difference afaik,
crontab -e just fires an editor to edit /etc/crontab (probably vi).



[ Reply to This | # ]
Run programs at startup via crontab
Authored by: hypert on Nov 08, '04 12:41:42PM

There's a big difference!

crontab -e edits your user crontab (stored in something like /var/spool/cron/username, which should NEVER be edited directly!)

/etc/crontab is the "system" cron job. The format of this file is slightly different, since it takes an extra argument before the actual command to be run. That argument is the username under which the command will run, If you were administering a Mac that saw a LOT of users, this is a nice plan to manage crons for all them (and the users can't edit them!).



[ Reply to This | # ]
How do I set a command line program to run on startup and stay in the background?
Authored by: iolaire on Nov 08, '04 08:50:19AM
Please is there a quick answer to this? - I want to run the python script: http://libgmail.sourceforge.net/ on startup to provide me with gmail pop access. Can I set a cron job that will load the python script in the background and keep it running? If so how? Right now I have it set up with an apple script to load the python script - but that is not very good because I have to leave the script running or else the python process will die. Thanks, iolaire

[ Reply to This | # ]
How do I set a command line program to run on startup and stay in the background?
Authored by: BulbVivid on Nov 08, '04 04:52:04PM

I don't really mess with all this stuff that is the subject of the parent of this thread, but after reading your post, I thought this app might help you out: http://sveinbjorn.vefsyn.is/platypus.

From the Web site: "Platypus is a powerful developer tool for creating application wrappers around scripts, i.e. for creating MacOS X applications that execute a bundled script. Scripts can thus be run seamlessly from the graphical window environment, making elegant Mac OS X-native applications from scripts."

It may be overkill, or it may be just what you need. . . .

Jason



[ Reply to This | # ]
Run programs at startup via crontab
Authored by: altano on Nov 22, '07 03:05:59PM

My @reboot crontab job seems to not be running anymore now that I've upgraded to Leopard. I've verified that the cron job is still there by doing 'crontab -l', and it is.

I'm using this to run ssh-agent at reboot. Does Leopard ignore @reboot cron jobs?



[ Reply to This | # ]