Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

5 Answers 5

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

Ref: http://manpages.ubuntu.com/manpages/precise/en/man5/crontab.5.html

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

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 script.sh 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
1  
I also agree with this, update-rc.d will let you setup initscripts for various runlevels. manpages.ubuntu.com/manpages/precise/en/man8/update-rc.d.8.html –  Vishnu Kumar Mar 21 '13 at 12:57

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

#!/bin/sh

case $1 in
     start)
        su - johan -c "screen -dm rtorrent"
       ;;
     stop)
        killall rtorrent
        killall screen
        ;;
esac
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 at 21:35

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

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 launch.sh in my home directory and adding it to the Ubuntu Startup applications list.

launch.sh contained the following and executed my script from within:

#!/bin/sh
screen -dmS 051 /home/{user}/{myscript} 

I discovered that the configuration file for the launch.sh startup application was contained in the file /home/{user}/.config/autostart/launch.sh.desktop 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

 
discard

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.