Launch program in screen after reboot

26,807

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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:
26,807

Related videos on Youtube

user142219
Author by

user142219

Updated on September 18, 2022

Comments

  • user142219
    user142219 over 1 year

    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.

  • McNisse
    McNisse about 11 years
    Note the comment, in the link, that all needed services might not be available when the cron daemon starts.
  • Vishnu Kumar
    Vishnu Kumar about 11 years
    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.h‌​tml
  • RParker
    RParker about 9 years
    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.
  • Josip Rodin
    Josip Rodin over 8 years
    There are other considerations with using Upstart, such as the /var/run/screen directory, please see unix.stackexchange.com/a/165647/103306