• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / Linux sysadmin tutorials linux system administrator / auto start after oom killer Mysql & php fpm nginx etc ubuntu wth systemd or cron job

auto start after oom killer Mysql & php fpm nginx etc ubuntu wth systemd or cron job

 

 

Table of Contents

Toggle
  • 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
  • mysql auto restart after oom killer without cron job with service
  • Restart php fpm after oom killer automatically
  • auto restart nginx after oomkiller
  • MySQL Service Stop although Restart=Always
  • Restaring service after specific time with systemd
  • Restaring service after specific time with cron

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

 

Primary Sidebar

Linux sysadmin tutorials linux system administrator

  • top 10 apt & apt-get commands (most used) apt vs apt-get
  • If-Else Statements in Shell Scripting
  • linux commands pdf (files & Directories, zip & unzip process, search etc)
  • Find Files with Specific Text on Linux grep find command
  • linux performance tuning inode limit file descriptors tco, kernel etc
  • Variables and Data Types in Shell Scripting
  • Top 10 most used Cat commands with examples (create, view, append files)
  • Ip tables / ufw / firewall d commands for block port ip rate limiting
  • Top 10 zip / tar commands to compress & extract files in linux
  • TOP 10 mv & cp commands in linux to move & copy files in Linux
  • Top 10 GREP Commands in linux to search files directory words strings
  • lsof netstat commands to know listening ports in linux 3 ways
  • Upgrade Ubuntu from 18.04 (19.10) to 20.04 LTS command line or gui server | desktop
  • 3 Ways (SCP, rsync, Sftp) linux server migration between two remote server apache nginx
  • linux system specs commands (CPU, Memory, Disk )speed, type. manufacture
  • linux sysctl command tweaks & hardening
  • linux security limits.conf deciding user limits process limits for nginx server
  • ulimit linux unlimited command unlimto set & know user limits open files file descriptor max user process etc.
  • red hat linux certification cost jobs salary syllabus courses fees
  • ufw firewall commads allow port enable disable ubuntu 20.04
  • ddos attack prevention
  • change ssh port in linux - avoid sshd ddos attacks
  • ping command
  • memcached install ubuntu wordpress
  • check linux version (lsb_release -a) ubuntu debian 32 or 64 bit
  • rsync command linux with examples comparison to scp
  • how to uninstall package in linux ubuntu rpm, yum apt-get
  • increase open file limit linux File descriptor ft nginx , mysql, lemp
  • remove repository ubuntu
  • htop commad memory details virtual vs shard vs resident
  • chown command in Linux with Examples
  • Kill PHP process
  • VIrtual Memory vs RSS Memory vs Shared memory in Linux
  • oom killer fixing it by configuration linux ubuntu
  • Install Lemp nginx mysql php fpm Stack on Debian 11 with repository
  • connect two remote servers linux command line
  • auto start after oom killer Mysql & php fpm nginx etc ubuntu wth systemd or cron job
  • load average Linux 1, 5, 15 min 2,4,8 cores explained
  • Control Structures in Shell Scripting
  • Shell Scripting Roadmap for Beginners to Advanced
  • awk commands with practical examples
  • Shell Scripting Tutorial for Beginners 🚀
  • find Command in Linux with Examples
  • sed Command in Linux with Examples (Beginner to Advanced)
  • Linux Text processing commands in with Examples
  • linux disk management commands
  • fdisk command in linux with examples
  • how to add a new disk in linux
  • Linux mount Command with Examples
  • fstab options with examples
  • Top 50 Shell Scripting Interview Questions and Answers
  • Linux Networking Interview Questions and Answers
  • Linux Networking Commands Cheat Sheet with Examples pdf
  • Netstat & SS Commands cheat sheet with examples Interview Questions
  • Nmap Cheat Sheet – Network Scanning & Security
  • Bash Brackets ([], (), {}, $( ), $(( ))) – Types, Uses & Examples

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

© 2025 - All Rights Reserved Disclaimer & Privacy Policy