bash – 将shell脚本放在systemd控件下
发布时间:2020-12-15 18:34:48 所属栏目:安全 来源:网络整理
导读:假设我有这样的 shell脚本: – #!/bin/sh# cherrypy_server.shPROCESSES=10THREADS=1 # threads per processBASE_PORT=3035 # the first port used# you need to make the PIDFILE dir and insure it has the right permissionsPIDFILE="/var/run/cherrypy/
假设我有这样的
shell脚本: –
#!/bin/sh # cherrypy_server.sh PROCESSES=10 THREADS=1 # threads per process BASE_PORT=3035 # the first port used # you need to make the PIDFILE dir and insure it has the right permissions PIDFILE="/var/run/cherrypy/myproject.pid" WORKDIR=`dirname "$0"` cd "$WORKDIR" cp_start_proc() { N=$1 P=$(( $BASE_PORT + $N - 1 )) ./manage.py runcpserver daemonize=1 port=$P pidfile="$PIDFILE-$N" threads=$THREADS request_queue_size=0 verbose=0 } cp_start() { for N in `seq 1 $PROCESSES`; do cp_start_proc $N done } cp_stop_proc() { N=$1 #[ -f "$PIDFILE-$N" ] && kill `cat "$PIDFILE-$N"` [ -f "$PIDFILE-$N" ] && ./manage.py runcpserver pidfile="$PIDFILE-$N" stop rm -f "$PIDFILE-$N" } cp_stop() { for N in `seq 1 $PROCESSES`; do cp_stop_proc $N done } cp_restart_proc() { N=$1 cp_stop_proc $N #sleep 1 cp_start_proc $N } cp_restart() { for N in `seq 1 $PROCESSES`; do cp_restart_proc $N done } case "$1" in "start") cp_start ;; "stop") cp_stop ;; "restart") cp_restart ;; *) "$@" ;; esac 从bash脚本中,我们基本上可以做三件事: >通过调用./cherrypy_server.sh start启动cherrypy服务器 我如何将这个shell脚本置于systemd的控制之下作为cherrypy.service文件(显然的目标是让systemd在重启机器时启动cherrypy服务器)? 这里参考systemd服务文件示例 – https://wiki.archlinux.org/index.php/Systemd#Using_service_file
我将这些用于Sick Beard和SabNZBd,两个python / cherrypy应用程序.不同的是知道何时使用’分叉’.这基本上告诉systemd主二进制文件将分叉内容,因此它必须从文件中猜出PID.
WantedBy只是定义需要启动的目标,将其视为运行级别.您还会注意到第二个定义使用一个目录来保存运行信息,这是因为它为每个已启动的守护进程创建了一个$process- $端口(您可能有许多守护进程由不同端口上的主守护进程生成). IMO你可以在ExecStart上添加脚本并确保它是forfor并为它添加一种方法来查找主PID文件,或者至少是一个PID文件,意味着’如果这已经死了,重启服务’. 也许理想的是为每个守护进程创建一个“简单”的服务文件? [Unit] Description=Internet PVR for your TV Shows After=cryptsetup.target [Service] ExecStart=/usr/bin/python2 /path/to/Sick-Beard/SickBeard.py Type=simple User=<user under which to run> Group=<group of said user> [Install] WantedBy=multi-user.target 而这一个是分叉的 [Unit] Description=Binary Newsreader After=cryptsetup.target [Service] ExecStart=/usr/bin/python2 /path/to/sabnzbd/SABnzbd.py -d -f /path/to/inifile --pid /run/sabnzbd Type=forking PIDFile=/run/sabnzbd/sabnzbd-8080.pid User=<user to run the process> Group=<group of said user> [Install] WantedBy=multi-user.target (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |