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 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)
share|improve this question

1 Answer 1

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|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.