Redhat – Execute a script while boot up in RHEL7

bootredhatrhel7ruby-on-rails

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.

Best Answer

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
Related Topic