0

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
Veljko
  • 1,708
  • 12
  • 40
  • 80

1 Answers1

1

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
  • it's not woriking. Still nothing is running. If I run it manually like this " ./startSanic2.sh & " then it is working while if I run it manully like this "./startSanic2.sh " then it is running only while the session is active. But with service it is not working. Please help :( – Veljko Apr 02 '19 at 16:42
  • netstat -tulpn|grep 9000 - with this I am checking if something is being run on the port which I configured or not. Because as I stated if I run it manually then I can see that something is being run on this port. Then I kill it and start as service but nothing happens:( thanks for helping me:( – Veljko Apr 02 '19 at 16:48
  • Also with ./startSanic2.sh & I am getting the pid number on which I can see that is being run on port 9000 – Veljko Apr 02 '19 at 16:49
  • What is the output of this command: `sudo journalctl -u san.service` –  Apr 02 '19 at 16:50
  • It seems that Service has issue with my environment variables because I have two environment variables in ~/.bashrc export REDIS_HOST=192.168.150.220 export REDIS_PORT=6379 variables are set for root : Please take a look at this full output it reports that there is some error that it cannot read my variable REDIS_PORT = int(os.getenv('REDIS_PORT')) TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' this is the URL for output we.tl/t-4sQrVaEGvi – Veljko Apr 02 '19 at 16:58
  • So it seems that is is struggling to read environment variable which is defined in .bashrc for root user. But I think this is really typical use case. Do you please have an idea how to overcome this? – Veljko Apr 02 '19 at 17:02
  • 1
    add `Environment="var1=val1"` under `[Service]` in `san.service` file –  Apr 02 '19 at 17:05
  • Thanks Remisa it seems it is working. I will reboot the server to check if it is being run succesfully!!! I marked your response as answer! If I may just one more question sorry - I put for After =network.target, but actually I need to run my service after service on port 6379 is run that is redis_6379 I have that in init.d and it is in rc5.d but for this redis there is nothing in /usr/lib/systemd/system . So how I can be sure that san.service is being run after redis_6379 . Please if you can answer me on this also. I marked answer and MANY MANY THANKS – Veljko Apr 02 '19 at 17:47
  • 1
    I guess adding `After=rc-local.service` in `[Unit]` section does the job. –  Apr 02 '19 at 17:57
  • Aha and then it will run everthing which it has in /etc/init.d (that is redis_6379) and everything which it has in rc5.d (that is S10network -> ../init.d/network S50redis_6379 -> ../init.d/redis_6379) When you said "I guess adding ...." I think you thought "replacing instead of After=network.target" correct? Thanks – Veljko Apr 02 '19 at 18:02
  • 1
    I said guess since never had a `systemd` service to be run after an `init.d` one. They are not compatible. I guess there is no exact solution for run after service `X` so they support `After=rc-local.service` to run after all `init.d` services. Another solution is checking port inside your script in a loop with `sleep` which obviously is not a clean solution. Best solution is running the other service with `systemd`. –  Apr 02 '19 at 18:09
  • Thank you once again. I think that this will do the trick After=rc-local.service – Veljko Apr 02 '19 at 18:14
  • can you please maybe assist me and with this other issue? Thanks. https://stackoverflow.com/questions/55493637/limit-logs-for-journalctl-services-in-systemd-centos-7 – Veljko Apr 04 '19 at 06:18