3

One of my server reboots every sunday and monday morning, whenever the server is up I need to do a service restart. Actually I call a script to execute itself (everytime manually). What I want to achieve is that it should be possible to invoke this script to get executed every time the server boots up. Is this possible?

The script I execute everytime is something like this:

cd /var/www/activefolder/current && RAILS_ENV=production script/delayed_job start

As you can see this script combines two tasks to finish this.

The server OS is Redhat EL 7. I guess until 6, OS uses init.d whereas RHEL7 introduces a more flexible option called systemd. But not sure whether it is possible to setup as I mentioned. Any help would be really appreciated.

Not sure whether I have given you all the requirements to answer this question. If not, please feel free to ask and I will update my question with the relevant information.

4 Answers 4

4

Based on Redhat's documentation Creating Custom Unit Files and systemd.exec — Execution environment configuration:

Not tested™

Create the file (touch>chmod>edit seems recommended, I don't known why):

touch /etc/systemd/system/activefolder.service
chmod 664 /etc/systemd/system/activefolder.service
vim /etc/systemd/system/activefolder.service

/etc/systemd/system/activefolder.service example:

[Unit]
Description=activefolder
After=network.target

[Service]
WorkingDirectory=/var/www/activefolder/current
ExecStart=/var/www/activefolder/current/script/delayed_job start
Type=forking
PIDFile=/run/activefolder.pid
Environment="RAILS_ENV=production"
#User=httpd
#Group=httpd

[Install]
WantedBy=default.target

Then:

systemctl daemon-reload
systemctl start name.service
1

You don't need to create a systemd service to run command at boot, it can be done with crontab.

Just add the command you need to run with @reboot <user_name> prefix to your /etc/crontab

In your case it would be:

@reboot root cd /var/www/activefolder/current && RAILS_ENV=production script/delayed_job start

Tested to work in CentOS 6/7 (requires vixie-cron, might not work with cronie)

0

Adding the commands at the end of /etc/rc.local still seems to work as well.

(Make sure that it is executable, though.)

-1

There is great flexibility in every Linux distro in general to execute scripts, code or other programs in initialization scripts, configuration files (via an include), etc..., just as long as the script, config file or whatever you use is in a security context that allows that.

Depending on what your code needs to acomplish, you might need to run it after a given service starts or before some other does. In some cases just placing it in the /etc/rc.local is not enough. If you need more control on the way your code executes due to its own nature, you will need to understand how the initialization scripts are fired, what run levels are, etc... This are good places to start:

For init based systems (RHEL < 7): https://www.linux.com/news/introduction-services-runlevels-and-rcd-scripts

For systemd systems (RHEL >= 7): http://0pointer.de/blog/projects/systemd-for-admins-1.html

1
  • Unfortunately, the article you link to is entirely related to init-style startup, and is thus completely irrelevant because RHEL7 is systemd-based.
    – MadHatter
    Jul 14, 2016 at 11:11

You must log in to answer this question.

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