Centos 7-来自/etc/systemd/system/san.service的服务未使用systemctl start san.service运行
我按照此链接中的说明进行操作: 但我有问题,因为我的服务没有运行 我创建了一个脚本startSanic2.sh,它调用startSanic.sh脚本(我需要这个脚本,因为当我手动启动它时&仅在这种情况下,会话未过期) 这是我的脚本startSanic2.shCentos 7-来自/etc/systemd/system/san.service的服务未使用systemctl start san.service运行,service,centos,systemd,init.d,systemctl,Service,Centos,Systemd,Init.d,Systemctl,我按照此链接中的说明进行操作: 但我有问题,因为我的服务没有运行 我创建了一个脚本startSanic2.sh,它调用startSanic.sh脚本(我需要这个脚本,因为当我手动启动它时&仅在这种情况下,会话未过期) 这是我的脚本startSanic2.sh # cat /opt/horses/startSanic2.sh #!/bin/bash cd /opt/horses ./startSanic.sh & 这是我的脚本startSanic.sh # cat /opt/horse
# 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
当我运行它时,什么都不会发生:(
我已签入/etc/systemd/system
我的san.com服务如下所示:
-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
[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
将
startSanic2.sh
更改为:
#!/bin/bash
/opt/horses/startSanic.sh
确保它是可执行的:
$ sudo chmod +x /opt/horses/startSanic2.sh
$ sudo chmod +x /opt/horses/startSanic.sh
还要确保startSanic.sh
是可执行的:
$ sudo chmod +x /opt/horses/startSanic2.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="var1=value1"
[Install]
WantedBy=default.target
[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
它没有问题。仍然没有运行任何东西。如果我像这样手动运行它“/startSanic2.sh&”,那么它可以工作,而如果我像这样手动运行它“/startSanic2.sh”,那么它只能在会话处于活动状态时运行。但对于服务,它不能工作。请帮助:(netstat-tulpn | grep 9000-通过这个,我正在检查我配置的端口上是否正在运行某些东西。因为正如我所说的,如果我手动运行它,那么我可以看到一些东西正在这个端口上运行。然后我杀死它并作为服务启动,但什么也没有发生:(感谢您的帮助:(添加
Environment=“var1=val1”)
在san.Service
文件中的[Service]
下我想在[Unit]中添加在=rc local.Service
之后
section做这项工作。我说猜测,因为从来没有一个systemd
服务在init.d
一个之后运行。它们不兼容。我想对于在服务之后运行X
没有精确的解决方案,所以它们支持after=rc local.service
在所有init.d
服务之后运行。另一个解决方案是使用sleep
在循环中检查脚本中的端口,这显然不是一个干净的解决方案。最好的解决方案是使用systemd
运行其他服务。