servicecentossystemdinit.dsystemctl

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

Solution

  • 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