Using cron to run a script each time the server starts

Often you want to run a script each time your server boots. For example, in the How to run VNC on startup guide we wrote a script to launch VNC. One way to get this script to run on boot is to add it as a cron job. This is very easy to do using Webmin. So, within Webmin click on System and then Scheduled Cron Jobs. Then click the Create a new scheduled cron job option at the top of the screen that opens.

Click the button next to the Execute cron job as and choose the username you created when you installed Ubuntu. Hint: your username appears in a Putty/Terminal session prompt. eg. yourusernameappearshere@MyMediaServer.

Note: If the script you've written needs to be run as root then obviously you'd choose root in the Execute cron job as section instead of your username. If the script needs to be run as any other user then obviously enter that username instead.

Enter the name of your script including the full pathname eg. /home/htkh/MyScripts/StartVNC.sh >/dev/null into the Command box, replacing htkh with your own username, MyScripts with the name of the folder you created to store your scripts and StartVNC.sh with the script name. The >/dev/null parameter will discard any output the script may produce. If your script actually needs to produce any output then it should be piped to a file. See the Monitor server temperatures scripts for an example of piping output to a file.

Enter a suitable description in the Description field.

In the When to Execute – Simple schedule drop-down list choose When system boots then click the Create button at the bottom of the screen.

Now's probably a good time to test it. I'd recommend first testing that you've set the job up correctly in Webmin. You can do this by clicking on the job you've just created from the long list of cron jobs. Then click the Run Now button at the bottom of the screen. You should see a message similar to the one you saw when you tested it from a Putty/Terminal session. If you don't then go back and check your settings.


Tag Cloud