Screen automatic startup

Have you ever wondered how to startup your scripts in screen upon boot?
I've wondered for a while, googled few times and when I found nothing nice I wrote this simple script.

It has few nice features:

- can run screen as given user
- check if screen/session is not already started.
- clean ups stale pid files
- it's a debian startup script
- reads command and user to run as from config file in $CFG dir.
- sets session name as defined in config. !new!

Comments and bugs are welcome to valqk to lozenetz dt net

Sample config /etc/screen-startup/run_site.cfg:

SCRIPT=/path/to/cron/script.sh
USER=siteuser
SCREEN_NAME=site_cronjob


Script name: screen-startup

#!/bin/sh
# /etc/init.d/screen-startup
#
### BEGIN INIT INFO
# Provides: screen-startup
# Required-Start: screen-cleanup
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
[ -z "$CFG" ] || ! [ -d "$CFG" ] && CFG='/etc/screen-startup/';
# Carry out specific functions when asked to by the system
startScreen() {
echo "Starting screens..."
for script in $CFG/*.cfg;
do
SCRIPT=`grep SCRIPT= $script|cut -f2 -d=`;
USER=`grep USER= $script|cut -f2 -d=`;
SCREEN_NAME=`grep SCREEN_NAME= $script|cut -f2 -d=`;
if [ -n "$SCRIPT" ] && [ -n "$USER" ]; then
if [ "x${SCREEN_NAME}" = "x" ]; then
sessName="`echo $SCRIPT|sed -e 's%/%_%g'`-$USER-AS"
else
sessName="${SCREEN_NAME}";
fi
if [ -f /var/run/screen/$sessName.pid ]; then
sessPid=`cat /var/run/screen/$sessName.pid`;
[ "x$sessPid" != "x" ] && [ `ps -p $sessPid|wc -l` -gt 1 ] && echo "$sessName alredy started ($sessPid)!!!" && continue;
echo "cleaning stale pid file: $sessName.pid"
rm /var/run/screen/$sessName.pid
fi
echo -n "Screen $SCRIPT for user $USER..."
/bin/su -c "/usr/bin/screen -dmS $sessName $SCRIPT" $USER
screenPid=`ps ax|grep "$sessName"|grep "$SCRIPT"|grep -v grep|awk '{print $1}'`
echo $screenPid > /var/run/screen/$sessName.pid
echo "done.";
fi
done
}
stopScreen() {
echo "Stopping screens..."
for script in $CFG/*.cfg;
do
SCRIPT=`grep SCRIPT= $script|cut -f2 -d=`;
USER=`grep USER= $script|cut -f2 -d=`;
SCREEN_NAME=`grep SCREEN_NAME= $script|cut -f2 -d=`;
sessName="`echo $SCRIPT|sed -e 's%/%_%g'`-$USER-AS"
if [ "x${SCREEN_NAME}" = "x" ]; then
sessName="`echo $SCRIPT|sed -e 's%/%_%g'`-$USER-AS"
else
sessName="${SCREEN_NAME}";
fi
if [ -f /var/run/screen/$sessName.pid ]; then
pidOfScreen=`cat /var/run/screen/$sessName.pid|cut -f 1 -d' '`;
pidOfBash=`cat /var/run/screen/$sessName.pid|cut -f 2 -d' '`;
if [ "x$pidOfBash" != "x" ] && [ `ps -p $pidOfBash|wc -l` -lt 2 ]; then
echo "Missing process $pidOfBash for screen $pidOfScreen. Cleaning up stale run file."
rm /var/run/screen/$sessName.pid;
continue;
else
echo -n "Screen: $SCRIPT for user $USER..."
kill $pidOfBash $pidOfScreen;
echo "done."
rm /var/run/screen/$sessName.pid;
fi
fi
done

}
case "$1" in
start)
startScreen;
;;
stop)
stopScreen;
;;
restart)
stopScreen;
startScreen;
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
exit 0


p.s. Edit: rev.1 of the script now supports SCREEN_NAME in config. When set you can resume screen with screen -s SCREEN_NAME (or part of it).