Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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

sh /var/www/scripts/
ctrl+ad (to detach)
share|improve this question

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:

# /etc/init.d/screensh

# Provides:
# 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: 
# Description:    This runs a script continuously in screen. 

case "$1" in

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

        echo "Restarting $screen"
        PID=`ps -ef | grep | grep -v grep | awk '{print $2}'`
        kill -9 $PID 
        sleep 15
        screen -dm sh /var/www/scripts/
        echo "Usage: $N {start|stop|restart}" >&2
        exit 1
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


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.