【问题标题】:Centos 7 - service from /etc/systemd/system/san.service not running with systemctl start san.serviceCentos 7 - 来自 /etc/systemd/system/san.service 的服务未使用 systemctl start san.service 运行
【发布时间】:2019-08-24 01:20:03
【问题描述】:

我按照此链接中的说明进行操作: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

【问题讨论】:

    标签: service centos systemd init.d systemctl


    【解决方案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
    

    【讨论】:

    • 它不工作。仍然没有运行。如果我像这样“./startSanic2.sh &”手动运行它,那么它正在工作,而如果我像这样“./startSanic2.sh”手动运行它,那么它仅在会话处于活动状态时运行。但是在服务方面它不起作用。请帮忙:(
    • netstat -tulpn|grep 9000 - 我正在检查我配置的端口上是否正在运行某些东西。因为正如我所说,如果我手动运行它,那么我可以看到这个端口上正在运行一些东西。然后我杀死它并作为服务启动但没有任何反应:(感谢你帮助我:(
    • san.service文件中的[Service]下添加Environment="var1=val1"
    • 我想在[Unit] 部分添加After=rc-local.service 就可以了。
    • 我猜是因为从来没有在init.d 之后运行systemd 服务。它们不兼容。我想在服务X 之后运行没有确切的解决方案,所以他们支持After=rc-local.service 在所有init.d 服务之后运行。另一种解决方案是使用sleep 在循环中检查脚本内的端口,这显然不是一个干净的解决方案。最好的解决方案是使用systemd 运行其他服务。
    猜你喜欢
    • 2019-03-29
    • 1970-01-01
    • 2019-02-08
    • 2021-01-28
    • 2018-08-13
    • 1970-01-01
    • 2019-08-24
    • 2021-01-28
    • 2023-03-10
    相关资源
    最近更新 更多