ubuntu 18.04 两种开机启动方法说明

/etc/init.d VS /etc/systemd/system 方法对比

/etc/init.d 方法比较旧。

/etc/systemd/system 比较新的方法

/etc/init.d 方法

我常用的是rclone自动挂载脚本。
使用方法:
1.复制内容,注意文本格式(UNIX).
2.将其重命名为rcloned(只要不是rclone就可以).
3.填好REMOTE和LOCAL,复制到/etc/init.d文件夹内.
4.运行初始化命令:bash /etc/init.d/rcloned init
5.使用 bash /etc/init.d/rcloned start 挂载.
6.使用 bash /etc/init.d/rcloned stop 卸载.

#!/bin/bash
### BEGIN INIT INFO
# Provides:          rclone
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start rclone at boot time
# Description:       Enable rclone by daemon.
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 
REMOTE='o_e3_fan:soft/linux'
LOCAL='/o'
CONFIG='/root/.config/rclone/rclone.conf'
DEMO='rclone'
 
[ -n "$REMOTE" ] || exit 1;
[ -x "$(which fusermount)" ] || exit 1;
[ -x "$(which $DEMO)" ] || exit 1;
 
case "$1" in
start)
  ps -ef |grep -v grep |grep -q "$REMOTE"
  [ $? -eq '0' ] && {
    DEMOPID="$(ps -C $DEMO -o pid= |head -n1 |grep -o '[0-9]\{1,\}')"
    [ -n "$DEMOPID" ] && echo "$DEMO already in running.[$DEMOPID]";
    exit 1;
  }
  fusermount -zuq $LOCAL >/dev/null 2>&1
  mkdir -p $LOCAL
  rclone mount $REMOTE $LOCAL --config $CONFIG --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 >/dev/null 2>&1 &
  sleep 3;
  DEMOPID="$(ps -C $DEMO -o pid=|head -n1 |grep -o '[0-9]\{1,\}')"
  [ -n "$DEMOPID" ] && {
    echo -ne "$DEMO start running.[$DEMOPID]\n$REMOTE --> $LOCAL\n\n"
    echo 'ok' >/root/ok
    exit 0;
  } || {
    echo "$DEMO start fail! "
    exit 1;
  }
  ;;
stop)
  DEMOPID="$(ps -C $DEMO -o pid= |head -n1 |grep -o '[0-9]\{1,\}')"
  [ -z "$DEMOPID" ] && echo "$DEMO not running."
  [ -n "$DEMOPID" ] && kill -9 $DEMOPID >/dev/null 2>&1
  [ -n "$DEMOPID" ] && echo "$DEMO is stopped.[$DEMOPID]"
  fusermount -zuq $LOCAL >/dev/null 2>&1
  ;;
init)
  fusermount -zuq $LOCAL
  rm -rf $LOCAL;
  mkdir -p $LOCAL;
  chmod a+x $0;
  update-rc.d -f $(basename $0) remove;
  update-rc.d -f $(basename $0) defaults;
  rclone config;
  ;;
esac
 
exit 0

再补充一个vm虚拟机开机的脚本

#!/bin/bash
prog="VM-TEST"
progname="VM-TEST"
RETVAL=0
# Edit the following to indicate the 'bin' directory for your installation
MDIR=/usr/bin

if [ ! -d "$MDIR" ]
then
 echo "Invalid directory $MDIR"
    exit 1
fi

start()
{
    #mv -f /var/log/vm.log /var/log/vm1.log
    echo "Starting $progname"
    cd $MDIR
    #su -l tao -c "vmrun -T ws start  ~/vmware/win10_test/win10_test.vmx nogui >>/var/log/vm.log 2>&1"
    su -l tao -c "vmrun -T ws start  ~/vmware/win10_test/win10_test.vmx nogui"
    #echo $? >>/var/log/vm.log
    #su -l ac -c "vmrun -T ws yyy.vmx nogui"
    #echo $? >>/var/log/vm.log
}

stop()
{
    #mv -f /var/log/vm_stop.log /var/log/vm_stop1.log
    #echo "Stopping $progname"
    cd $MDIR
    #vmrun suspend "win10_test.vmx" #nogui >/var/log/vm_stop.log
    #vmrun suspend "yyy.vmx" #nogui >>/var/log/vm_stop.log
    su -l tao -c "vmrun -T ws stop ~/vmware/win10_test/win10_test.vmx" soft
}
status()
{
    cd $MDIR
    vmrun list
}
case "$1" in
 start)
       start
         ;;
 stop)
      stop
        ;;
 status)
      status
        ;;
    *)
     echo "Usage: $prog {start|stop|status}"
     exit 1
     ;;
esac

exit $RETVAL
~
~
~
~

/etc/systemd/system 方法

启动方法:

开机自启动 jupyter

sudo systemctl enable jupyter.service

启动 jupyter

sudo systemctl start jupyter.service

这个我常用的是jupyter 开机脚本

[Unit]
Description=Jupyter Notebook

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/opt/anaconda3/envs/jupyter/bin/jupyter-notebook --config=/home/tao/.jupyter/jupyter_notebook_config.py
WorkingDirectory=/home/tao/jupyterNotebook
Restart=always
RestartSec=10
#KillMode=mixed
User=tao
Group=tao

[Install]
WantedBy=multi-user.target

#  sudo systemctl enable jupyter.service
#  sudo systemctl start jupyter.service

参考连接:

https://www.moerats.com/archives/481/
https://blog.csdn.net/Devper/article/details/54089342

© 版权声明
THE END
在时间中留下我的脚印
分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情图片

    暂无评论内容