Instead of resuming the session to execute the command, you have to send the command with -X and specify the needed window with -p (though I only need to specify a window on my VPS, the root does it automatically, kinda strange).
Code:
screen -AmdS foo
screen -S foo -p 0 -X stuff "echo hi
"
(the newline is needed)
Instead of echo hi, you gonna write ./server.sh of course (to encapsulate your LD_PRELOAD env vars, easier/faster testing etc.)
I wrote such a script for my own "PHP hosting", so you could reuse/fit it:
/root/shell/service.sh
PHP Code:
count_services()
{
echo $(screen -ls | grep service | wc -l)
}
#are_screens_running
#echo $?
are_screens_running()
{
linesOfScreen="$(screen -ls | wc -l)"
if [ $linesOfScreen != "2" ]; then
echo "screens running"
return 1
fi
echo "NO screens running"
return 0
}
service_start()
{
# prevent multiple services
if [ $(count_services) -ge 1 ]; then
echo "Service already running!"
return 0
fi
if ! [ -a ./server.sh ]; then
echo "Configure Service First!";
return 0
fi
# this way the session will be quitted after crash:
# so i could make a php-daemon which is watching the sessions
# start the session
screen -AmdS service #./server.sh
# this way the session is still open after crash:
screen -S service -p 0 -X stuff "./server.sh
"
echo "Service started!"
}
service_stop()
{
if [ $(count_services) = 0 ]; then
echo "Service already stopped!"
return 0
fi
#screen -r service -X quit
# delete ALL screens named "service"
for i in $(screen -ls | grep service | awk '{print $1}'); do screen -d -r $i -X quit; done
echo "Service stopped!"
}
service_status()
{
path=~
script=server.sh
if [ ! -f "$path/$script" ]; then
echo "Service not configured!"
return 0
fi
if [ $(count_services) = 0 ]; then
echo "Service not running!"
return 0
fi
echo "Service is running!"
return 1;
}
service_restart()
{
service_stop
service_start
}
case $1 in
"start")
service_start
;;
"stop")
service_stop
;;
"status")
service_status
;;
"restart")
service_restart
;;
esac
Example:
Code:
k_deathrun@Debian-70-wheezy-64-LAMP:~$ /root/shell/service.sh status
Service not running!
k_deathrun@Debian-70-wheezy-64-LAMP:~$ /root/shell/service.sh start
Service started!
k_deathrun@Debian-70-wheezy-64-LAMP:~$ /root/shell/service.sh status
Service is running!
k_deathrun@Debian-70-wheezy-64-LAMP:~$ /root/shell/service.sh stop
Service stopped!
k_deathrun@Debian-70-wheezy-64-LAMP:~$ /root/shell/service.sh status
Service not running!