Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
init.d script to start user's daemons on system boot
kenneth@KWF3:/etc/init.d$ ls
apparmor hwclock-save pppd-dns stop-bootlogd
apt-cacher-ng irqbalance procps stop-bootlogd-single
atd killprocs rc tftpd-hpa
atop lm-sensors rc.local udev
bootlogd mdadm rcS udev-finish
console-setup module-init-tools README udevmonitor
cron networking reboot udevtrigger
dbus network-interface rsync ufw
dmesg network-interface-security rsyslog umountfs
dns-clean nmbd screen-cleanup umountnfs.sh
failsafe-x ondemand sendsigs umountroot
fancontrol plymouth single urandom
grub-common plymouth-log skeleton userstart
halt plymouth-splash smbd vsftpd
hostname plymouth-stop squid3 x11-common
hwclock postfix ssh
kenneth@KWF3:/etc/rc0.d$ ls
K20apt-cacher-ng K20userstart README S31umountnfs.sh S60umountroot
K20atop K25mdadm S20sendsigs S35networking S90halt
K20postfix K30squid3 S30urandom S40umountfs
kenneth@KWF3:/etc/rc2.d$ ls
README S20fancontrol S25mdadm S70dns-clean S99ondemand
S20apt-cacher-ng S20postfix S30squid3 S70pppd-dns S99rc.local
S20atop S20userstart S50rsync S99grub-common
#! /bin/sh
### BEGIN INIT INFO
# Provides: UserStart
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts user daemons from ~/cron/startup.sh
# Description: Users are able to start any personal daemons they
# like by adding them to ~/cron/startup.sh
#
### END INIT INFO
# Author: Kenneth Finnegan <kennethfinnegan2007@gmail.com>
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Run every user's ~/cron/startup.sh script"
NAME=userstart
SCRIPTNAME=/etc/init.d/$NAME
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
cat /etc/passwd |
while read PASSWDLINE; do
TARGETUSER="`echo $PASSWDLINE | awk -F ':' '{print $1}' `"
TUSERHOME="`echo $PASSWDLINE | awk -F ':' '{print $6}' `"
STARTSCRIPT="$TUSERHOME/cron/startup.sh"
if [ -x $STARTSCRIPT ]; then
cd $TUSERHOME
su -l -c $STARTSCRIPT $TARGETUSER &
fi
done
return 0
}
#
# Function that stops the daemon/service
#
do_stop()
{
# An obvious extension would be to call ~/cron/shutdown.sh here...
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
return 0
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment