you can check mysql status every minute (with cron) and restart if it is crashed:
if you want auto start php fpm and mysql after restart
systemctl enable php-fpm
systemctl enable mysql
mysql auto restart after crash with cron job
* * * * * /bin/systemctl status mysqld.service > /dev/null || /bin/systemctl start mysqld.service
The double pipe || means OR and will execute the 2nd command if the first command fails somehow. (Returns an exit code greater than zero.)
ubuntu debian vesrsions
* * * * * service mysql status > /dev/null || service mysql start
If checking every single minute is too frequent, a five-minute interval will do just as fine for smaller websites:
*/5 * * * * service mysql status > /dev/null || service mysql start
mysql auto restart after oom killer without cron job with service
by default mysql restart on failure enabled with mysql 8 versions.
Restart php fpm after oom killer automatically
Edit your php-fpm.service file. Eg, type nano /lib/systemd/system/php-fpm.service.
Add the following 2 lines at the bottom of [Service]:
Restart=on-failure
RestartSec=5s
Restart systemctl: systemctl daemon-reload.
my case php7.4
cat /lib/systemd/system/php7.4-fpm.service
or
nano /lib/systemd/system/php8.2-fpm.service
add
Restart=on-failure
RestartSec=45s
[Service]
Type=notify
ExecStart=/usr/sbin/php-fpm7.4 –nodaemonize –fpm-config /etc/php/7.4/fpm/php-fpm.conf
ExecStartPost=-/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74
ExecStopPost=-/usr/lib/php/php-fpm-socket-helper remove /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
nano /lib/systemd/system/php7.4-fpm.service in case 8.1 (nano /lib/systemd/system/php8.1-fpm.service)
and add below
Restart=on-failure
RestartSec=5s
systemctl daemon-reload (reloads systemd files.)
sudo systemctl restart php7.4-fpm or sudo systemctl restart php8.2-fpm
Php will now automatically restart whenever it fails. You should ocassional check the logs to make sure it’s not failing often.
You may also wish to do the same for nginx: /lib/systemd/system/nginx.service
ubnutu
service mysql status
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
Active: active (running) since Sat 2023-09-02 03:23:07 UTC; 1 months 5 day>
Docs: man:mysqld(8)
cat /lib/systemd/system/mysql.service
[Unit]
Description=MySQL Community Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network-online.target
Wants=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=notify
ExecStartPre=+/usr/share/mysql-8.0/mysql-systemd-start pre
ExecStart=/usr/sbin/mysqld
TimeoutSec=0
LimitNOFILE = 10000
Restart=on-failure
RestartPreventExitStatus=1
# Always restart when mysqld exits with exit code of 16. This special exit code
# is used by mysqld for RESTART SQL.
RestartForceExitStatus=16
# Set enviroment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1
mariadb
mariadb set restart always
auto restart nginx after oomkiller
service nginx status
nano /lib/systemd/system/nginx.service
add Restart=always
0r
Restart=on-failure
save and reload the nginx
It is a feature of SystemD. Override existing unit file for NGINX by running systemctl edit nginx then paste in:
[Service]
Restart=always
Save.
If NGINX is down due to, e.g. the OOM killer, it will be restarted after dying. If you have a configuration error in NGINX, it will not be restarted, of course.
MySQL Service Stop although Restart=Always
Solution: Just add some seconds before make service start to freeup memory or cpu)- in my case I’ve added 45 seconds
{/etc/systemd/system/multi-user.target.wants/mariadb.service}
Restart=always
RestartSec=45s
Do NOT forget to run the following commands
sudo systemctl daemon-reload
sudo systemctl restart mariadb.service
The RestartSec
option configures the amount of time to wait before restarting the service. Here, it’s set to one second to override the default value of 100ms.
The default delay between executions is 100ms (RestartSec
) which causes the rate limit to be reached very fast.
systemd tries to restart multiple times (StartLimitBurst
) and stops trying if the attempt count is reached within StartLimitIntervalSec
. Both options belong to the [unit]
section.
[Unit]
Description=Your Daemon Name
StartLimitIntervalSec=300
StartLimitBurst=5
[Service]
ExecStart=/path/to/executable
Restart=on-failure
RestartSec=1s
[Install]
WantedBy=multi-user.target
StartLimitIntervalSec
and StartLimitBurst
. They are both useful for configuring when Systemd should stop trying to restart a service. The former specifies a time interval in seconds, while the latter specifies the maximum amount of times that is allowed for the service to be restarted within the specified interval.
Restaring service after specific time with systemd
RuntimeMaxSec option to terminate a systemd service after running for a certain amount of time. Also, we set the Restart descriptor to always. This option makes sure that the service is always restarted.
$ sudo vi my-service1.service
[Unit]
Description=Simple service
[Service]
ExecStart=/usr/bin/python3 /root/app.py
RuntimeMaxSec=180s
Restart=always
[Install]
WantedBy=multi-user.target
This service will execute a basic python program that runs indefinitely. Moreover, this service will terminate after 180 seconds and restart again:
Restaring service after specific time with cron
every 15 minutes
*/15 * * * * /usr/bin/systemctl restart my-service.service