1

I have the following script in the /etc/systemd/system/ folder.

# Start the ruby on rails app on boot
[Unit]
Description=Start Ruby Online Ordering Application

[Service]
Type=simple
RemainAfterExit=yes
Username=root
PIDFile=/home/deviant/www/tmp/pids/server.pid
WorkingDirectory=/home/deviant/www
ExecStart=/home/deviant/www/run.sh
TimeoutSec=300

[Install]
WantedBy=multi-user.target

with a permission of -rw-r--r-- 1 root root

The run.sh script contains

#!/bin/sh
service httpd stop
rails s -p 80 -d

with a permission of -rwxrwxr-x 1 root root

In short, it is supposed to start a Ruby application upon restart of a server, however, it does not appear to be working.

Where can I check to see system error logs? What would cause it NOT to start upon a server restart?

I tried changing the script to:

[Service]
Type=oneshot
RemainAfterExit=yes
PIDFile=/home/deviant/www/tmp/pids/server.pid
WorkingDirectory=/home/deviant/www
ExecStart=/home/deviant/www/run.sh
TimeoutSec=300

And I now get on systemctl enable....

Unknown lvalue 'Username' in section 'Service' ...has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.

I'm not sure what this even means. What does it mean?

3
  • 4
    Please read the answers to this existing question. You're using Type=simple, but it looks like your run.sh script will start two processes and then exit. This makes systemd think that your service failed, and it will kill off the httpd and rails child processes, with the assumption that they are just garbage left by the failed service. Type=simple is for processes that won't exit until the administrator runs the command to stop the service.
    – telcoM
    Commented Jan 23, 2020 at 14:25
  • @telcoM then what should it be?
    – Kendall
    Commented Jan 24, 2020 at 17:53
  • Does this answer your question? How do I figure out why my systemctl service didn't start on CentOS 7?
    – user232326
    Commented Jan 29, 2020 at 14:57

2 Answers 2

2

If your Ruby application conflicts with the httpd service, I don't see why you should keep that service running just to stop it in your run.sh: just run systemctl disable httpd once and it won't even be started.

Now you can eliminate your run.sh altogether and make your service file with Type=simple and ExecStart=/full/path/to/rails s -p 80. Note the removal of the -d option; this is intentional and works well with Type=simple.

For insurance, you can add Conflicts=httpd.service to tell systemd that your service and httpd cannot run at the same time.

So your /etc/systemd/system/my-ruby-application.service should look like this:

# Start the ruby on rails app on boot
[Unit]
Description=Start Ruby Online Ordering Application
Conflicts=httpd.service

[Service]
Type=simple
Username=root
PIDFile=/home/deviant/www/tmp/pids/server.pid
WorkingDirectory=/home/deviant/www
ExecStart=/some/full/path/rails s -p 80
TimeoutSec=300

[Install]
WantedBy=multi-user.target

To make systemd restart your service if it fails for some reason, add a Restart= option that is suitable to your requirements (see Table 2 in man systemd.service). If you do that, it's recommended to also add a RestartSec=<sensible restart delay> to avoid bogging down your server with repeated restart attempts in 0.1s intervals (the default) if your service happens to keep failing after each restart attempt, perhaps because you edited its configuration file and accidentally made a mistake.

Once you've edited your my-ruby-application.service file, you should run systemctl daemon-reload, then systemctl disable httpd and systemctl stop httpd if you didn't already do that, and finally systemctl enable my-ruby-application and systemctl start my-ruby-application.

1
  • Enable/disable imply a daemon-reload, of course. And the PIDFile is just dross.
    – JdeBP
    Commented Jan 24, 2020 at 21:19
0

Did you reloaded the systemctl daemon ?

systemctl daemon-reload  
systemctl enable service_name

I know that Centos distros keep boot logs in

cat /var/log/boot.log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .