linux - 在 Linux 中使用 systemd 启动脚本

标签 linux bash startup boot systemd

我可以这样启动下面的服务吗,运行一次没有显示错误,但是下面的服务器脚本没有运行!

ln /lib/systemd/aquarium.service aquarium.service
systemctl daemon-reload
systemctl enable aquarium.service
systemctl start aquarium.service

谢谢

水族馆服务:

[Unit]
Description=Start aquarium server

[Service]
WorkingDirectory=/home/root/python/code/aquarium/
ExecStart=/bin/bash server.* start
KillMode=process

[Install]
WantedBy=multi-user.target

这里是server.sh脚本

#!/bin/bash

PID=""

function get_pid {
   PID=`pidof python ./udpthread.py`
}

function stop {
   get_pid
   if [ -z $PID ]; then
      echo "server is not running."
      exit 1
   else
      echo -n "Stopping server.."
      kill -9 $PID
      sleep 1
      echo ".. Done."
   fi
}


function start {
   get_pid
   if [ -z $PID ]; then
      echo  "Starting server.."
      ./udpthread.py &
      get_pid
      echo "Done. PID=$PID"
   else
      echo "server is already running, PID=$PID"
   fi
}

function restart {
   echo  "Restarting server.."
   get_pid
   if [ -z $PID ]; then
      start
   else
      stop
      sleep 5
      start
   fi
}


function status {
   get_pid
   if [ -z  $PID ]; then
      echo "Server is not running."
      exit 1
   else
      echo "Server is running, PID=$PID"
   fi
}

case "$1" in
   start)
      start
   ;;
   stop)
      stop
   ;;
   restart)
      restart
   ;;
   status)
      status
   ;;
   *)
      echo "Usage: $0 {start|stop|restart|status}"
esac

最佳答案

尝试使用“Type=forking”并使用完整的文件名。

[Unit]
Description=Start aquarium server

[Service]
WorkingDirectory=/home/root/python/code/aquarium/
Type=forking
ExecStart=/bin/bash server.sh start
KillMode=process

[Install]
WantedBy=multi-user.target

如果它不起作用,请发布此命令的输出:

# journalctl -u aquarium.service

关于linux - 在 Linux 中使用 systemd 启动脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15146049/

相关文章:

bash - 如果我按 Ctrl+C,psql 会破坏我的 shell

linux - 如何将输出直接用作命令

linux - 创建 MongoDB 启动脚本

linux - 使用sed从文件中提取数据并 append 到Linux中的目标文件

linux - 邮件被发送到本地域而不是远程域

bash - fasta 文件 : replace header with filename

linux - C++ 生成文件安装 : start program at boot and reboot

linux - 在特定终端中通过 Perl 运行 shell 命令

linux - 在 linux bash 脚本中使用 tee 随机更新文件失败

r - R启动时如何覆盖2GB内存限制