Restart
With systemd you can set up your service to Restart
on certain conditions:
systemctl cat mysql.service | grep Restart # Check current status. cat PATTERN... Show files and drop-ins of specified units
Probably you wish to move from on-abort
to on-failure
, on [Service]
section.
sudo EDITOR=nano systemctl edit mysql.service
Other useful commands:
sudo systemctl daemon-reload # Reload systemd manager configuration. `systemctl edit` automatically does this for you.
systemctl cat mysql.service # unit configuration
systemd-delta # Check the whole system changes
OOMScoreAdjust
On the same place, you may also want to adjust systemd OOMScoreAdjust:
Sets the adjustment value for the Linux kernel's Out-Of-Memory (OOM) killer score for executed processes. Takes an integer between -1000 (to disable OOM killing of processes of this unit) and 1000 (to make killing of processes of this unit under memory pressure very likely).
# Useful options not previously available in [mysqld_safe]
Kernels like killing mysqld when out of memory because its big.
Lets temper that preference a little.
OOMScoreAdjust=-600
Read also this excellent answer.
You can also reduce the memory usage of MySQL/MariaDB by tuning some options like max_connections
, innodb_buffer_pool_size
and innodb_buffer_pool_instances
(probably on /etc/mysql/mariadb.conf.d/50-server.cnf
).
\\
in front of your shebang? If it's there to keep it from looking like a comment, then it's unnecessary. Shebangs are treated special like by Bash, as in they don't need to be escaped because they aren't comments in the sense that a comment is a piece of code that is not evaluated at all. – Alexej Magura Apr 21 '14 at 06:37[ ... ]
ortest <TEST>
in Bash. They're deprecated syntax. Use[[ ... ]]
instead. Only use[ ... ]
and/ortest <TEST>
when[[ ... ]]
is not available. – Alexej Magura Apr 21 '14 at 06:43if ! (service mysql status | grep 'mysql start/running' &>/dev/null); then sudo service mysql restart; fi
What this does, is it starts up a subshell, whereinservice mysql status | grep 'mysql start/running' &> /dev/null
gets run, the return (exit) status of said subshell then gets passed to the if-statement, which then checks to see if it is non-zero, and if it is not non-zero then it runs thethen
block. – Alexej Magura Apr 21 '14 at 07:17safe_mysqld
or something like this (I don't remember details anymore) that was used to start the service. This script itself was monitoring the service and restarting it if it happens to crash. Is it no more the case? – raj Sep 10 '21 at 07:59