Score:0

Add system password while running the .sh file

cn flag

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.

Raffa avatar
jp flag
`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.
Score:0
jp flag

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.

Score:0
sm flag

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.

Raffa avatar
jp flag
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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.