Centos 7-/etc/systemd/system/san.service中的服务未与systemctl一起运行启动san.service

时间:2019-04-02 16:23:43

标签: service centos systemd init.d systemctl

我遵循了以下链接的说明:https://www.thegeekdiary.com/centos-rhel-7-how-to-make-custom-script-to-run-automatically-during-boot/

但是我遇到了问题,因为我的服务没有运行。

我创建了一个脚本startSanic2.sh,该脚本调用startSanic.sh脚本(我需要这样做是因为当我使用&手动启动它时(仅在这种情况下会话未过期)

这是我的脚本startSanic2.sh

# cat /opt/horses/startSanic2.sh 
#!/bin/bash
cd /opt/horses
./startSanic.sh &

这是我的脚本startSanic.sh

# cat /opt/horses/startSanic.sh 
#!/bin/bash
gunicorn horses.server:app --bind 0.0.0.0:9000 --worker-class sanic.worker.GunicornWorker --reload

运行此命令(./startSanic2.sh)后,它将在端口9000上成功运行。

startSanic.sh和startSanic2.sh的权限为755

然后我创建了新的san.service

[root@testnn2 system]# cat /etc/systemd/system/san.service
[Unit]
Description=Description for sample script goes here
After=network.target

[Service]
Type=simple
ExecStart=/opt/horses/startSanic2.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

我运行以下命令:

systemctl daemon-reload

我运行此命令

# systemctl enable san.service
Created symlink from /etc/systemd/system/default.target.wants/san.service to /etc/systemd/system/san.service

当我运行它时-没有任何反应:(

systemctl start san.service

我检查了并在/etc/systemd/system中 我的san.service看起来像这样:

-rw-r--r--  1 root root  193 Apr  2 18:07 san.service

请在这个问题上为我提供帮助,为什么什么也不做。

更新:环境变量

/etc/systemd/system/san.service的内容:

[Unit]
Description=Description for sample script goes here
After=network.target

[Service]
Type=simple
ExecStart=/opt/horses/startSanic2.sh
TimeoutStartSec=0
Environment="var1=value1"

[Install]
WantedBy=default.target

1 个答案:

答案 0 :(得分:1)

startSanic2.sh更改为:

#!/bin/bash

/opt/horses/startSanic.sh

确保它是可执行的:

$ sudo chmod +x /opt/horses/startSanic2.sh

还要确保startSanic.sh是可执行的:

$ sudo chmod +x /opt/horses/startSanic.sh

重新加载守护程序并启用它:

$ sudo systemctl daemon-reload
$ sudo systemctl enable san.service
$ sudo systemctl start san.service

重新启动计算机。

更新:

san.service中设置环境变量:

[Unit]
Description=Description for sample script goes here
After=network.target

[Service]
Type=simple
ExecStart=/opt/horses/startSanic2.sh
TimeoutStartSec=0
Environment="REDIS_HOST=192.168.150.220"
Environment="REDIS_PORT=6379"

[Install]
WantedBy=default.target
相关问题