分类
互联网

AutoRun McMyAdmin at reboot/start (Linux/Ubuntu)

1. Login as root, create a file as
vim /etc/init.d/McMyAdmin.sh
2. Copy and paste the following content into it

#!/bin/sh
# /etc/init.d/McMyAdmin
# version 0.0.1 2014-11-24

### BEGIN INIT INFO
# Provides:          McMyAdmin
# Required-Start:    $local_fs $remote_fs screen-cleanup $syslog
# Required-Stop:     $local_fs $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start McMyAdmin at boot
# Description:       Start McMyAdmin at boot.
### END INIT INFO
#Settings
USERNAME='mcserver'
SERVICE='McMyAdmin'
MCAPATH="/home/$USERNAME/McMyAdmin"
INVOCATION="cd $MCAPATH && screen -dmS $SERVICE ./MCMA2_Linux_x86_64"

ME="$(whoami)"
as_user() {
        if [ "$ME" = "$USERNAME" ] ; then
                bash -c "$1"
        else
                su - "$USERNAME"  -c "$1"
        fi
}


case "$1" in
        start)
                if pgrep -u $USERNAME -f $SERVICE > /dev/null
                then
                        echo "McMyAdmin is already started on screen McMyAdmin"
                else
                        echo "Starting $SERVICE ..."
                        cd $MCAPATH
                        as_user "$INVOCATION"
                        sleep 7
                        if pgrep -u $USERNAME -f $SERVICE > /dev/null
                        then
                                echo "McMyAdmin started on screen $SERVICE"
                        else
                                echo "Error! Could not start $SERVICE"
                        fi
                fi
                ;;
        stop)
                if pgrep -u $USERNAME -f $SERVICE > /dev/null
                then
                        echo "Stopping $SERVICE..."
                        as_user "screen -X -S $SERVICE kill"
                        sleep 7
                else
                        echo "$SERVICE was not running."
                fi
                if pgrep -u $USERNAME -f $SERVICE -a > /dev/null
                then
                        echo "Error! $SERVICE could not be stopped."
                else
                        echo "$SERVICE is stopped."
                fi
                ;;
        *)
                echo "Usage: /etc/init.d/McMyAdmin_autostart {start|stop}"
                exit 1
                ;;
esac
exit 0

Pleae note that you need to adjust the USERNAME and MCAPATH.
3. :wq to save and quit
4. Change the mode of the file
chmod +x /etc/init.d/McMyAdmin.sh
5. create soft link to startup
update-rc.d McMyAdmin.sh defaults 99
Note that you can remove it by
update-rc.d -f McMyAdmin.sh remove
6. reboot to test.
7. If it is not work, try to run as root, with the following command
/etc/init.d/McMyAdmin start

发表回复

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据