工作之余用写的。就是把 一个脚本A 做成 一个服务A,然后每次开机自动调用这个服务。
脚本A直接写在下面这个脚本里面,运行下面这个脚本一步一步来就好了,这里是开机自启动jypyter-lab的一个命令。
如果有什么问题,去检查一下 我写入的log 文件夹中的文件。 有时候 要一些模块类似 pip install typing_extensions。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #!/bin/bash
read -sp "请输入你的root密码: " root_password echo
echo "$root_password" | sudo -S -p '' bash -c '
# 在 /home/jlsf/ 下创建一个 log 文件夹,不会覆盖已存在的文件夹 echo "在 /home/jlsf/ 下创建一个 log 文件夹,不会覆盖已存在的文件夹" mkdir -p /home/jlsf/log if [ $? -ne 0 ]; then echo "Failed to create log folder." >&2 exit 1 else echo "Log folder created or already exists." fi
# 检查是否存在 /home/jlsf/Auto_start_jupyter.sh 脚本,如果不存在则创建 echo "检查是否存在 /home/jlsf/Auto_start_jupyter.sh 脚本,如果不存在则创建" if [ ! -f /home/jlsf/Auto_start_jupyter.sh ]; then cat << 'SCRIPT' > /home/jlsf/Auto_start_jupyter.sh #!/bin/bash /usr/bin/nohup /home/jlsf/.local/bin/jupyter lab --no-browser --ip=0.0.0.0 --allow-root >> /home/jlsf/log/myscript.log 2>&1 & # /home/jlsf/.local/bin/jupyter lab --no-browser --ip=0.0.0.0 --allow-root >> /home/jlsf/log/myscript.log 2>&1 SCRIPT if [ $? -ne 0 ]; then echo "Failed to create Auto_start_jupyter.sh script." >&2 exit 1 else echo "Auto_start_jupyter.sh script created." fi else echo "Auto_start_jupyter.sh script already exists." fi
# 尝试添加 Auto_start_jupyter.sh 脚本权限 echo "尝试添加 Auto_start_jupyter.sh 脚本权限。" chown jlsf:jlsf /home/jlsf/Auto_start_jupyter.sh chown jlsf:jlsf /home/jlsf/log chmod 776 /home/jlsf/Auto_start_jupyter.sh if [ $? -ne 0 ]; then echo "Failed to set permissions on Auto_start_jupyter.sh script." >&2 exit 1 else echo "Permissions set on Auto_start_jupyter.sh script." fi
# 创建 systemd 服务单元文件 echo "创建 systemd 服务单元文件" if [ ! -f /etc/systemd/system/Auto_start_jupyter.service ]; then cat << 'SERVICE' > /etc/systemd/system/Auto_start_jupyter.service [Unit] Description=Jupyter Lab Service After=network.target
[Service] Type=forking ExecStart=/home/jlsf/Auto_start_jupyter.sh StandardOutput=append:/var/log/my_program.log StandardError=append:/var/log/my_program.log Restart=always User=jlsf WorkingDirectory=/home/jlsf
[Install] WantedBy=multi-user.target SERVICE if [ $? -ne 0 ]; then echo "Failed to create Auto_start_jupyter.service." >&2 exit 1 else echo "Auto_start_jupyter.service created." fi else echo "Auto_start_jupyter.service already exists." fi
# 重新加载 systemd 配置 echo "重新加载 systemd 配置" systemctl daemon-reload if [ $? -ne 0 ]; then echo "Failed to reload systemd daemon." >&2 exit 1 else echo "Systemd daemon reloaded." fi
# # 重置服务的启动限制计数 # echo "重置服务的启动限制计数" # systemctl reset-failed # if [ $? -ne 0 ]; then # echo "Failed to reset-failed systemd service." >&2 # exit 1 # else # echo "Systemd service reset-failed success." # fi
# 启用并启动服务 echo "启用并启动服务" systemctl enable Auto_start_jupyter.service if [ $? -ne 0 ]; then echo "Failed to enable Auto_start_jupyter.service." >&2 exit 1 else echo "Auto_start_jupyter.service enabled." fi echo "现在开始开启服务" systemctl start Auto_start_jupyter.service # if [ $? -ne 0 ]; then # echo "Failed to start Auto_start_jupyter.service." >&2 # exit 1 # else # echo "Auto_start_jupyter.service started." # fi
# 检查服务状态 echo "检查服务状态" systemctl status Auto_start_jupyter.service
'
|