Skip to content

Instantly share code, notes, and snippets.

@errordeveloper
Created September 1, 2011 16:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save errordeveloper/1186575 to your computer and use it in GitHub Desktop.
Save errordeveloper/1186575 to your computer and use it in GitHub Desktop.
## Sample screenrc for rtorrent
## put it in /etc/screen.d/
## and chown for the user you
## who wish to run session
source /etc/screenrc
chdir /home/stuff/dump/
screen -t rtorrent -d . -s .
#!/sbin/runscript
# Ilya Dmitrichenko < errordeveloper - at -at- g m a i l -dot- com
# Distributed under the terms of the GNU General Public License v2
# Originally written for Gentoo, but should work on other platforms :)
# http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=2&chap=4
depend() {
use net debug
}
start() {
## SVCNAME is the name of this file in /etc/init.d/
## so one can creat /etc/extra-screens.d/ and put
## the screenrc files there, then simlink the files
## /etc/init.d/screen -> /etc/init.d/extra-screens
for SCREENRC in /etc/${SVCNAME}.d/* ; do
SESSION="$(basename $SCREENRC)"
## I don't think there may be a security issue,
## provided that users will not be have write
## permission in /etc/screen.d/ and if anyone
## gained access to mod the session file, they
## are in already anyhow!
BELONGS="$(stat $SCREENRC --printf=%U)"
MYSHELL="$(getent passwd $BELONGS | cut -d: -f7)"
COMMAND="/usr/bin/screen -- -U -D -m -c ${SCREENRC} -S ${SESSION} -t ${SESSION}"
## Why on earth would one write this ???
#HOMEDIR="$(getent passwd $BELONGS | cut -d: -f6)"
ebegin "Starting ${SVCNAME} session ${SESSION} for ${BELONGS}"
PIDFILE="/var/run/${SVCNAME}.${BELONGS}.${SESSION}.pid"
start-stop-daemon \
--env TERM="rxvt" \
--env HOME="~${BELONGS}" \
--env SHELL="${MYSHELL}" \
--env SCREEN_SESSION=${SESSION} \
--user $BELONGS \
--chdir "~${BELONGS}" \
--make-pidfile \
--background \
--pidfile=${PIDFILE} \
--exec ${COMMAND}
eend $?
done
#screen -li || /bin/true
}
stop() {
## Perhaps we should determin this by pidfiles ...
## but this way is not bad either!
for SCREENRC in /etc/${SVCNAME}.d/* ; do
SESSION="$(basename $SCREENRC)"
BELONGS="$(stat $SCREENRC --printf=%U)"
PIDFILE="/var/run/${SVCNAME}.${BELONGS}.${SESSION}.pid"
PROCESS="$(cat ${PIDFILE})"
if [ -e /proc/${PROCESS}/status ]; then
grep -i "Name:" /proc/${PROCESS}/status | grep -iq "screen" || continue
ebegin "Stopping ${SVCNAME} session ${SESSION} for ${BELONGS} (PID: ${PROCESS})"
## It will CERTAINly kill the righ screen!
CERTAIN="${PROCESS}.${SESSION}"
env TERM="urxvt" \
start-stop-daemon \
--user ${BELONGS} \
--exec /usr/bin/screen -- -S $CERTAIN -X quit
eend $?
fi
rm -f $PIDFILE
done
}
@errordeveloper
Copy link
Author

It should work on other Linux distros too, not only Gentoo :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment