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.
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):
/etc/systemd/system/activefolder.service
example:Then:
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 withcronie
)Adding the commands at the end of
/etc/rc.local
still seems to work as well.(Make sure that it is executable, though.)
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