Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

Centos 7 - service from /etc/systemd/system/san.service not running with systemctl start san.service

I followed instructions from this link: https://www.thegeekdiary.com/centos-rhel-7-how-to-make-custom-script-to-run-automatically-during-boot/

But I have issue because my service is not being run.

I created one script startSanic2.sh which invokes startSanic.sh script (I need this because when I start it manually with & only in this case session is not expired)

This is my script startSanic2.sh

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

This is my script 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

After this is run (./startSanic2.sh) it is being run successfully on port 9000.

startSanic.sh and startSanic2.sh have permissions 755

I then created new 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

I run this command:

systemctl daemon-reload

And I run this command

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

When I run it - nothing happens :(

systemctl start san.service

I checked and in /etc/systemd/system my san.service looks like this:

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

Please assist me on this issue why nothing is being run.

Update: environment variables

Content of /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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change startSanic2.sh to:

#!/bin/bash

/opt/horses/startSanic.sh

Make sure it is executable:

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

Also make sure startSanic.sh is executable:

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

Reload daemon and enable it:

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

Reboot machine.

Update:

Set environment variables in 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...