This question already has answers here:
sudo echo "something" >> /etc/privilegedFile doesn't work [duplicate]
(15 answers)
Closed 4 years ago.
I need that in the middle of a script in bash a line be executed as root, I have this code:
#!/bin/bash
...
EOF
if [[ $x = true ]] || [[ $y= "On" ]]; then
echo -e "0 8 * * * root " >> /etc/crontab
I need the echo written in the crontab by the root user, who has a password.
Currently it does not write as it is executed by the ubuntu user.
Thank you.
I propose this:
echo -e "0 8 * * * root " | sudo tee -a /etc/crontab
When your script encounters the sudo for the tee, it will ask for your password or, if you already gave your password for sudo just recently, it will just execute the task. (Of course, if this user doesn't need to type a password for sudo, it will never ask for it.)
Using tee -a instead of shell redirection works around the fact that redirections are performed by the current shell, so adding sudo to the command will not affect the redirect (and then it will fail due to missing permissions).
Related
This question already has answers here:
Pipe multiple verbatim lines into command in bash
(2 answers)
Closed 2 months ago.
Here is the case. I have bash script that requires a couple of command to be executed. First of all, it requires sudo, then answer (y/n) and then password one more time. What I want to do is I want to execute it in one command.
Let's say I have my bash script - myscript.sh. This script requires sudo to be executed. So, to execute it in one line I can write:
echo 'mypassword' | sudo -S bash myscript.sh
And this will work. But after script is executed I need to answer y and type password one more time. How can I do that?
Here is what I have tried:
printf '%s\n mypassword y mypassword' | sudo -S bash myscript.sh
echo 'y\nmypassword\n' | echo 'mypassword' | sudo -S bash myscript.sh
And there were a couple more of what I have tried, but it didn't work.
You can:
printf '%s\n' mypassword y mypassword | ...
( echo mypassword; echo y; echo mypassword ) | ...
This script requires sudo to
Note that typing that on the command line or in a script will publish your password. Instead, configure sudoers to allow the user to login without a password. Consider configuring credential caching in sudoers, so you don't have to type password twice.
This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 months ago.
For security purposes, I have been writing a script that daily create dump, and manage them for some daily dumps, and some monthly dumps. I have a weird issue that I can clearly understand. When I run the script above manually ./save_db.sh evrything works. But I did add this in crontab of the same user (which is not root).
cd /home/debian/script && sh save_db.sh
The script is this:
#!/bin/bash
nom_dump="$(date +%H:%M-%d%m%y)"
mysqldump -u debian --all-databases | gzip -c > /home/debian/bdd_dump/journalier/"$nom_dump".sql.gz
sauv_mensuelle="$(date +%d)"
if [ $sauv_mensuelle == "01" ]
then
cp "$nom_dump".sql.gz /home/debian/bdd_dump/mensuel
compte_sauv="$(ls /home/debian/bdd_dump/mensuel | wc -l)"
if (( $compte_sauv > 6 ))
then
clean_mensuel="$(ls -t /home/debian/bdd_dump/mensuel/ | tail -1)"
rm /home/debian/bdd_dump/mensuel/"$clean_mensuel"
fi
fi
compte_semaine="$(ls /home/debian/bdd_dump/journalier/ | wc -l)"
if (( $compte_semaine > 7 ))
then
clean_daily="$(ls -t /home/debian/bdd_dump/journalier/ | tail -1)"
rm /home/debian/bdd_dump/journalier/"$clean_daily"
fi
When crontab runs it, the last part is not working as intended, but I can't know why. Dumps older than 7 days are not being removed
Thanks :)
The way you are calling this script:
cd /home/debian/script && sh save_db.sh
Is environment dependent. Chmod +x your script and replace the call in crontab with
/path/to/script/my-script-name.sh
You only need one shell process to run, not two, and you don't need to change your working directory to invoke it.
I am not a linux expert and I have a problem I do not manage to solve. I am sorry if it is obvious.
I am trying to execute a bash script in a cron table on a raspberry pi, and I don't manage to get it work.
Here is the example script I want to execute:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
plouf=$( ps -aux | grep reviews | wc -l)
if [[ "$plouf" == 1 ]] ;
then
echo "plouf" >> /home/pi/Documents/french_pain/crontest.txt
fi
My script in the cron consist in starting a script if there is no progam with review in its name running. To test I am just appending "plouf" to a file. I count the number of line of ps -aux | grep reviews | wc -l , and if there in only one line I do append "plouf" in a file.
Here is my crontab:
crontab -l
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
* * * * * sudo /home/pi/Documents/french_pain/script2.sh
The script do work when I do ./ script2.sh or /home/pi/Documents/french_pain/script2.sh directly in terminl: it add a "plouf" to the file.
I came across this page and tried different possibilities, by setting my path as the path given by env, and by explicitly setting the shell in the cron. But still not working.
What I am I doing wrong ?
To answer Mark Setchell comment:
raspberrypi:~/Documents/french_pain $ sudo /home/pi/Documents/french_pain/script2.sh
raspberrypi:~/Documents/french_pain $ cat crontest.txt
plouf
and cron is running:
raspberrypi:~/Documents/french_pain $ pgrep cron
353
I manage to do simple jobs like
* * * * * /bin/echo "cron works" >> /tmp/file
I tried with the direct path to the commands:
plouf=$( /bin/ps -aux | /bin/grep 'R CMD.*reviews' | usr/bin/wc -l)
if [[ "$plouf" == 1 ]] ;
then
/bin/echo "plouf" >> /home/pi/Documents/french_pain/crontest.txt
fi
without any luck. The permission for the file:
-rw-rw-rw- 1 root root 6 juil. 3 23:30 crontest.txt
I tried deleting it, and did not work either.
help !
I guess you trying this as "pi" user then 'sudo' won't work unless you have allowed nopasswd:all or using a command that is able to handle the password that Sudo requires from stdin in this case. The example below is dangerous since it will not require any password for sudo command anymore but since you wanted to use sudo in cronie:
Example 1:
With default /etc/sudoers below example will create an empty file:
* * * * * sudo ls / > ~/cronietest.txt
Try add below in /etc/sudoers at bottom (obs: do not use pi as username on a rpi):
pi ALL=(ALL) NOPASSWD:ALL
Now try again to add below in crontab
* * * * * sudo ls / > ~/cronietest.txt
It works!
Example 2:
This is more safe, add this to sudoers file for allow 'command' for "pi" user without any password when sudo is executed:
pi ALL= NOPASSWD: /bin/<command>
Example 3:
Without editing sudoers file, this is another example that will work (this is dangerous since your password is stored in cron file as plaintext)
* * * * * echo "password" | sudo -S ls / > ~/cronietest.txt
I did not find the reason why the sript was not working, but I finally found a way to make it work thanks to this post: I used shell script instead of bash:
The file script3.sh
#!/bin/sh
if ps -ef | grep -v grep | grep 'reviews'; then
exit 0
else
echo "plouf" >> /home/pi/Documents/french_pain/crontest.txt
fi
together with
* * * * * /home/pi/Documents/french_pain/script3.sh
in my crontab did the work I wanted.
This question already has answers here:
How to run two commands with sudo?
(11 answers)
Closed 7 years ago.
Can we use heredocs to run multiple commands using sudo?
I am facing an issue (need to pass password for every command) while running
multiple commands:
echo 'password'| sudo -S ls
echo 'password'| sudo -S cat /var/log/abc.log
Can anyone help me how to automate this in a script? Like:
echo 'password' | sudo -S << ENDBLOCK
ls
cat
ENDBLOCK
you can run sh -c ..., but remember to quote properly.
sudo sh -c 'id; echo another command ; id'
sudo must see this as a single argument for the sh command.
Of course you can use new line instead of semicolon:
sudo sh -c '
echo "I am root"
id
echo "another command"
id
'
One way is to write a script with all the things you need to do with sudo and then run the script with sudo.
you could put all your commands in a script. Then
sudo ./script.sh
put permissions for script.sh in /etc/sudoers.d; that way you'll never need to type your password again (for that script)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I get this error if: Expression Syntax.
on this script
#!/bin/sh
pmon_num=`ps -ef | grep pmon | wc -l`
echo $pmon_num
if [ $pmon_num -gt 1 ]
then
#code
fi
If I run the script like this: /tmp/script.sh - it works. But the error comes from crontab. Crontab line is like that:
* * * * * /tmp/script.sh > /tmp/log.log 2>&1 &
ls -l on /tmp/script.sh:
-rwxr--r-- 1 oracle dba 705 Dec 12 07:10 /tmp/script.sh
Cant understand the problem.
EDIT:
I also tried to change the if statement to:
if [ 1 -gt 1 ]
And I still got the error.
I don't think that's a Bash error message. So, cron is apparently invoking a different shell (maybe csh?). To fix that, and ensure that this is running with an appropriate interpreter, change this:
#!/bin/sh
to this:
#!/bin/bash
Scripts executed inside cron doesn't have PATH nor do they source system wide or user profile. The script didn't find the commands ps, grep and wc thus $pmon_num is empty.
Either set the PATH in the script or fully qualify the pathnames of the commands.
On a side note, why use grep and pipe to wc -l? You can just use grep -c since you are not expecting several pmons in one line and it will return the count.
EDIT:
Adding modified code for you.
#!/bin/sh
pmon_num=`/bin/ps -ef | /bin/grep -c pmon`
echo $pmon_num
if [ $pmon_num -gt 1 ]
then
#code
fi
EDIT:
Here's how you can test what your cron thinks of PATH and unqualified commands.
#!/bin/sh
echo $PATH > /tmp/cronjob.out
which ps >> /tmp/cronjob.out 2>&1
which grep >> /tmp/cronjob.out 2>&1
which wc >> /tmp/cronjob.out 2>&1
After cron has executed this script check the content of /tmp/cronjob.out.
Just remember that sh != bash! If you want bash then tell it that! #!/usr/bin/env bash!
Since it's working in your current enviroment, try fixing your SHELL, PATH variables, and interpreter and see if you still get the error. e.g. run
printf "SHELL=$SHELL\nPATH=$PATH\n* * * * * /bin/sh /tmp/script.sh > /tmp/log.log 2>&1 &\n"
That will give you some output like
SHELL=/bin/tcsh
PATH=/usr/lib:/usr/bin:etc...
* * * * * /bin/sh /tmp/script.sh > /tmp/log.log 2>&1 &
But containing your SHELL and PATH. Then edit whichever crontab again with crontab -e and replace it with above input and see if it's working.
Replace:
if [ $pmon_num -gt 1 ]
then
fi
With:
if ( $pmon_num > 1 ) then
endif