1

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 !

denis
  • 5,580
  • 1
  • 13
  • 40
  • What happens if you do this in the Terminal `sudo /home/pi/Documents/french_pain/script2.sh` – Mark Setchell Jul 03 '20 at 21:23
  • it works perfectly, adds a "plouf" to my file – denis Jul 03 '20 at 21:29
  • Try running `type sudo` and adding the full path to `sudo` into your crontab. – Mark Setchell Jul 03 '20 at 21:31
  • no, it does not work. Removing `sudo ` either. – denis Jul 03 '20 at 21:33
  • Nearly out of ideas! Try adding a blank line at the end of your crontab. – Mark Setchell Jul 03 '20 at 21:48
  • I did that too, I saw that here : https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working . It is so frustrating, I don't get it. Thank you for trying – denis Jul 03 '20 at 21:50
  • Last idea... maybe something here https://gist.github.com/satishask/2c892a10144053ba1687ce9cb2748dd5 – Mark Setchell Jul 03 '20 at 21:52
  • thank you for the link. I will try all point. – denis Jul 03 '20 at 22:01
  • It's not clear if the problem is a) your script is not running at all, or b) it is running but cannot produce output. So try making the second line of your script `touch /tmp/ran-at-$SECONDS` – Mark Setchell Jul 03 '20 at 22:29
  • 1
    grepping the output of `ps` is unpredictable -- depending on timing, when you do something like `/bin/ps -aux | /bin/grep 'R CMD.*reviews'`, the `ps` command might or might not print the `grep` command among its output, and the `grep` will match its own entry, adding one to the number of matches. I'd recommend using `pgrep`, which avoids this problem. You can replace the entire pipeline with `plouf=$(pgrep -c 'R CMD.*reviews')` – Gordon Davisson Jul 04 '20 at 00:16
  • @GordonDavisson thank you for pointing that out. It did not solve my problem, but was really worth learning. Thanks – denis Jul 05 '20 at 21:39
  • @MarkSetchell I found a solution finally (see my answer below), so I stopped searching why this was not working. Thank you so much for helping. I learned a lot – denis Jul 05 '20 at 21:39

2 Answers2

1

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
wuseman
  • 1,259
  • 12
  • 20
  • I wish it was the solution, but calling `* * * * * /home/pi/Documents/french_pain/script2.sh` (i.e. without `sudo`) in the crontab does not work either, whereas calling `/home/pi/Documents/french_pain/script2.sh` in terminal does work – denis Jul 04 '20 at 19:59
  • Raspbian already allows this by default. Specifically, `/etc/sudoers.d/010_pi-nopasswd` contains "`pi ALL=(ALL) NOPASSWD: ALL`". – Gordon Davisson Jul 05 '20 at 22:37
0

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.

denis
  • 5,580
  • 1
  • 13
  • 40