Run command with sudo in bash shell

How can I run a command in a shell script using sudo? This script will be executed by a cron job, so there should be no human intervention to enter the password manually.

+3


source to share


4 answers


echo 'password' | Sudo -S command



+3


source


Enter cronjob in

sudo crontab -e 

      



then the whole script will be executed as root by default without the need for a password.

+4


source


Why not run the script itself as root in cron, as this is basically what sudo will do? Are you talking about the user crontab?

0


source


I think you are trying to do this:

       user# crontab -e
        * * * * * sudo ./code_here

      

But every time the script is called to provide credentials

So, you can try this:

       user# sudo su
        root# crontab -e
       * * * * *    ./code goes here

      

Thus: it will run with administrator rights.

0


source







All Articles