Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am running several headless Ubuntu 12.04 boxes. My typical usage involves logging into each machine via a putty session and executing a script using screen.

After logging in via putty, I execute the following from the command line: screen /home/user/"script"

My question is:

Is there any way to get my script/program to run automatically after a reboot? I'm trying to prevent a loss of productivity in a power loss/automatic reboot situation.

share|improve this question

You can use cron to do this for you.

  • Simply do crontab -e in terminal
  • It will open in your selected editor, where you can type
    @reboot <your command>
    (make sure you give full path for all executables)
  • Save the file and exit


share|improve this answer
Note the comment, in the link, that all needed services might not be available when the cron daemon starts. – McNisse Mar 21 '13 at 10:21
I'm not sure exactly when this broke, but in my experience, screen is not immediately available in Ubuntu 14.04 (and newer?). So, you might need to make it @reboot sleep 5; <your command> or something. I haven't played around with the exact timing, though. – Powerlord Apr 30 '15 at 16:54

The official way to do this is by creating init scripts. These scripts have a start, stop, restart and status command and can be configured to run these commands at specific times. So when you switch to leven 6 (reboot) it will stop the service before rebooting and if configured correctly it will automaticly run start on this script when switching to runlevel 3. You can find these scripts in /etc/init.d. If you create the file correctly (so it must be able to handle a start and a stop parameter) you can run update-rc.d defaults to set it to automaticly boot. Make sure you did chmod +x on the script first to make it executable. The folder /etc/init.d contains loads of examples of how this is done.

There is also the /etc/rc.local file, this file runs after the system has fully booted, and you can just add your own commands.

Personally I prefer the init.d scripts because you can make sure that the program closes gracefully, thus preventing possible dataloss or other errors. But for one command, or somethhing that can't really break /etc/rc.local will do just fine.

share|improve this answer
I also agree with this, update-rc.d will let you setup initscripts for various runlevels. – Vishnu Kumar Mar 21 '13 at 12:57

Modify your script in such a way that it can be invoked as an init script or, better yet, an upstart job. With some luck, you can keep it pretty much as it is, and have it run at boot by simply adding an upstart configuration to /etc/init. (Take any small .conf file there as a template.)

Two things you'll need to be aware of:

  1. Your script is invoked with root privileges. Drop them, so that your script runs with the appropriate user privileges (your user identity, in this case). Do this by invoking the final screen command in your script using su

    su -c "screen ..." - username.

  2. When started at boot, your script is run without a controlling terminal, whereas you currently run it from a shell. Since you use screen I assume you wish to re-attach to the terminal running script on your next login. If so, this may require passing extra options when you invoke screen.

share|improve this answer
There are other considerations with using Upstart, such as the /var/run/screen directory, please see – Josip Rodin Dec 18 '15 at 10:15

I have this in script to start rtorrent in a screen at boot time:


case $1 in
        su - johan -c "screen -dm rtorrent"
        killall rtorrent
        killall screen
exit 0

You can combine this with Vishnus answer if you prefer crontab, <your command> = screen -dm /usr/home/"script".

share|improve this answer
+1 Old school crontab, yep. – eduncan911 Jan 18 '14 at 21:35

I tried all of the above, with no luck. The script I am running needs X to launch.

What did ultimately work though it may have been the easy way out, was creating a small script called in my home directory and adding it to the Ubuntu Startup applications list. contained the following and executed my script from within:

screen -dmS 051 /home/{user}/{myscript} 

I discovered that the configuration file for the startup application was contained in the file /home/{user}/.config/autostart/ and I was able to recreate both files in the same locations on my other boxes and recreate the success. Thanks to all who tried to help. I'm sweating a few less bullets having figured out a workaround for this.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.