The better/safer alternatives are:
Disable the mysql.service
unit in systemd
like so:
sudo systemctl disable mysql.service
This way, the Mysql server will not be started automatically at boot but can be started manually anytime when needed.
Add your command/s to root
s crontab to run after boot like so:
sudo crontab -e
then, add a line using systemd
's service operations like so:
@reboot sleep 20 && /usr/bin/systemctl stop mysql.service
or, using /etc/init.d/mysql
like this:
@reboot sleep 20 && /etc/init.d/mysql stop
or, even using /usr/sbin/service
like this:
@reboot sleep 20 && /usr/sbin/service mysql stop
Notice the use of full path to executable files is a good measure in crontab and the addition of the sleep
call to delay the execution until the service has started and became responsive.
However, sudo
has an -S, --stdin
option:
-S, --stdin
Write the prompt to the standard error and read the password from
the standard input instead of using the terminal device.
That can be used like so:
echo 'PASSWORD' | /usr/bin/sudo --stdin /usr/bin/systemctl stop mysql.service
or, like so:
echo 'PASSWORD' | /usr/bin/sudo --stdin /etc/init.d/mysql stop
or, like so:
echo 'PASSWORD' | /usr/bin/sudo --stdin /usr/sbin/service mysql stop
However, this method is not recommended as it will save and pass your password in plane text not to mention other safety hazards involved with this method.
Notice as well that with sudo
and if your commands are nested with ||
, &&
or ;
operators/separators, you will need to use sudo
on both sides of those separators when needed for each single command or otherwise, the side that doesn't have sudo
before its command will execute with normal user privileges and not super user privileges.