1

How to make my bash script as service in CentOS 7? following is failing.

$ cat /etc/systemd/system/mybash.service
[Unit]
Description=mybash Service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/root/runme.sh
Restart=on-abort

[Install]
WantedBy=multi-user.target

$ systemctl start mybash
$ systemctl status mybash -l
mybash.service - mybash Service
   Loaded: loaded (/etc/systemd/system/mybash.service; disabled)
   Active: inactive (dead)
.....

$ cat /root/runme.sh 
#!/bin/bash
echo "Start the node server"
a=$(pgrep -f "a.js");
kill $a;
a=$(pgrep -f "b.js");
kill $a;
node /home/www/html/a.js &
node /home/www/html/b.js &
5

Do it properly.

  • Don't use forever under service managers. They already do this.
  • Don't use pgrep and kill under service managers. They do this better.
  • Don't use start-stop-daemon under service managers. They do this better.

That entire mechanism with that shell script is wrongheaded to start with, and fixing its readiness protocol mismatch would be simply papering over some vast cracks. You don't need any of that entire cobbled-together-poor-imitation-of-daemon-management script at all. You have a service manager that can do that stuff directly and safely. Use it.

# /etc/systemd/system/node@.service
[Unit]
Description=node service for %i.js
Documentation=https://unix.stackexchange.com/questions/207658/
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/node /home/www/html/%i.js
Restart=on-abort
SyslogIdentifier=node-%i

[Install]
WantedBy=multi-user.target

Fix the path to /usr/local/bin/node as appropriate. The usual commands then apply:

  • systemctl preset node@a.service node@b.service
  • systemctl start node@a.service node@b.service
  • systemctl status node@a.service node@b.service

Further reading

  • I used your method and it worked for nodejs but its failing for to make turnserver. Can you download and install this then try to make it run you will see it fails turnserver.open-sys.org/downloads/v3.2.5.7/… – YumYumYum Jun 6 '15 at 8:03
  • 2
    A comment is not the proper place to ask another question about a completely different software package; "it fails" is a useless problem description; and I am not going to download arbitrary softwares from unknown parts of the World Wide Web and run them, especially not to find an undisclosed bug of unspecified nature in an unknown location. When you have a question ask it properly with How to Ask, complete with a description of what you did and what happens when you run it per the standard litany. – JdeBP Jun 6 '15 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.