Only answering the service restart part. I came across Monit as well, but on CentOS 7 systemd takes care of all that for you. You just need to add these two lines to the .service file (if they're not there already):
Restart=always
RestartSec=3
Run man systemd.service
for reference.
If you want to create a custom systemd service, it's pretty straightforward to write your own service file. See the example below, for a custom http server.
Start the editor with a new service file:
vim /etc/systemd/system/httpd.service
And add the following content, which you can edit as required:
[Unit]
Description=My httpd Service
After=network.target
[Service]
Type=simple
User=root
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PERLLIB=/perl
ExecStart=/bin/httpd /etc/httpd.conf
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
I want it to start automatically on boot:
systemctl enable httpd
Tell systemd about the changes and start the service:
systemctl daemon-reload
systemctl start httpd
And now you can see the status:
systemctl status httpd
There are many options for Restart
. The following is an excerpt from the man page:
Restart=
Configures whether the service shall be restarted when the
service process exits, is killed, or a timeout is reached...
Takes one of no, on-success, on-failure, on-abnormal, on-watchdog,
on-abort, or always. If set to no (the default), the service will not
be restarted. If set to on-success, it will be restarted only when
the service process exits cleanly. In this context, a clean exit means
any of the following:
• exit code of 0;
• for types other than Type=oneshot, one of the signals SIGHUP,
SIGINT, SIGTERM, or SIGPIPE;
• exit statuses and signals specified in SuccessExitStatus=.
If set to on-failure, the service will be restarted when the process
exits with a non-zero exit code, is terminated by a signal (including
on core dump, but excluding the aforementioned four signals), when an
operation (such as service reload) times out, and when the configured
watchdog timeout is triggered.
The restart can be tested by sending an appropriate signal to the application. See the manual at man kill
for details.