0

My problem is when my pc start I want to disable mysql service with below command

sudo service mysql stop || service mysql stop

in both case system is asking for system password so I am entering the password manually everytime

I make one .sh file with above command

So how do I set & pass my system password with above script so do dont' enter password everytime when this script run.

1
  • 1
    sudo has the -S, --stdin option for that …. But, you can disable the service in systemd or add your command to root's crontsb as much better/safer alternatives.
    – Raffa
    Apr 29, 2023 at 8:15

2 Answers 2

0

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 roots 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.

0

To avoid having to enter your system password every time the script runs, you can use the sudoers file to grant permission to the user to execute the sudo command without entering a password.

Here are the steps you can follow:

Open the sudoers file using the visudo command:

sudo visudo

Add the following line at the end of the file, replacing username with your actual username:

username ALL=(ALL) NOPASSWD: /usr/sbin/service mysql stop

This line grants permission to the user to execute the service mysql stop command as a superuser without entering a password.

Save and exit the file. With this configuration, when you run your script, the sudo command will not ask for a password, and the script will be able to stop the MySQL service without any manual intervention.

1
  • This method, however, is not limited to the script/startup command described in the OP ... i.e. any other script or user entered command with sudo service mysql stop will immediately execute anytime all the time which might not be desired or at least requires a warning included in the answer.
    – Raffa
    Apr 29, 2023 at 10:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .