How can I use init.d (or some other method) to run a screen script on boot up?

7,735

I currently use a screen script for minecraft.

This is how I do it: screen -dmS.

Exact lines in the script :

as_user "cd $MCPATH && screen -dmS $SCREEN $INVOCATION"
as_user "screen -list | grep '\.$SCREEN' | cut -f1 -d'.' | tr -d -c 0-9 > $pidfile"

You could probably just use screen -dm it might work perfectly for you.

As far as the "run it on boot" You can make an init.d script.

sudo vi /etc/init.d/screensh:

#!/bin/bash
# /etc/init.d/screensh

### BEGIN INIT INFO
# Provides:   screen.sh
# Required-Start: $local_fs $remote_fs
# Required-Stop:  $local_fs $remote_fs
# Should-Start:   $network
# Should-Stop:    $network
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:    Screen.sh 
# Description:    This runs a script continuously in screen. 
### END INIT INFO

case "$1" in

  start)
        echo "Starting screen.sh"
        screen -dm sh /var/www/scripts/screen.sh
        ;;
  stop)
        echo "Stopping screen.sh"
        PID=`ps -ef | grep screen.sh | grep -v grep | awk '{print $2}'`
        kill -9 $PID 
        ;;

  restart|force-reload)
        echo "Restarting $screen"
        PID=`ps -ef | grep screen.sh | grep -v grep | awk '{print $2}'`
        kill -9 $PID 
        sleep 15
        screen -dm sh /var/www/scripts/screen.sh
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart}" >&2
        exit 1
        ;;
esac
exit 0

And then run sudo update-rc.d screensh defaults.

Try that, play around with it if it doesn't work, but it should... don't really have a test system to play around with at the moment.

Share:
7,735

Related videos on Youtube

Gina Lim
Author by

Gina Lim

Updated on September 18, 2022

Comments

  • Gina Lim
    Gina Lim almost 2 years

    I have a shell script that runs inside of a screen session and loops the script continuously every 10 minutes (never ends). I was wondering how I might initiate the screen session, run the shell script, then detach from it on boot.

    Right now I run this:

    screen
    sh /var/www/scripts/screen.sh
    ctrl+ad (to detach)