Skip to content

Instantly share code, notes, and snippets.

@elcepi

elcepi/cgminer

Last active Dec 23, 2015
Embed
What would you like to do?
My cgminer init.d script using tmux
#! /bin/sh
### BEGIN INIT INFO
# Provides: cgminer
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts cgminer
# Description: Start script for cgminer
### END INIT INFO
# Author: Jose Espinosa @jose-espinoas
# Donations: 1SJXbsiphSVw133p5wsGa3qM2vfDmuD5M
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.
# You need to install tmux ('apt-get install tmux') a screen multiplexer.
# If you want to access the cgminer console go to the machine and type:
# tmux
# Instructions:
# 1. Add your own parameters on the PARAM variable below.Most of the
# parameters needed can be determined in file
# ${HOME}/.cgminer/cgminer.conf
# 2. Copy this file to /etc/init.d/ by typing
# sudo cp cgminer /ect/init.d
# 3. Mark to run at boot time
# sudo update-rc.d cgminer ename
# Now if your computer restarts cgminer will restart :)
# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
USER=pi
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="cgminer start script"
NAME=cgminer
DAEMON=/usr/local/bin/${NAME}
SCRIPTNAME=/etc/init.d/$NAME
PARAM="-S /dev/ttyUSB0"
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
. /lib/lsb/init-functions
is_running()
{
ps ax | grep -v grep | grep $1 > /dev/null
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
tmux_session()
{
su --command "tmux list-session 2>/dev/null | grep $1 > /dev/null" ${USER}
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
# Exit if the package is not installed
#[ -x "$DAEMON" ] || exit 0
#
# 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
is_running ${DAEMON} && return 1
tmux_session ${NAME}
if [ $? -eq 0 ]; then
ATTACH_OPTION="new-window -t ${NAME} "
else
ATTACH_OPTION="new-session -s ${NAME} "
fi
INIT_SCRIPT=true su --command "/usr/bin/tmux ${ATTACH_OPTION} -d 'cd /home/${USER} ; ${DAEMON} ${PARAM}'" ${USER} || return 0
}
#
# Function that stops the daemon/service
#
do_stop()
{
# 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
is_running ${DAEMON} && killall ${DAEMON}
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)
is_running ${DAEMON}
if [ $? -eq 0 ]; then
log_success_msg "${NAME} is running"
exit 0
else
log_failure_msg "${NAME} is not running"
exit 1
fi
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
sleep 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
;;
*)
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
exit 3
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment