2

I am new with Crontab and Linux. I am trying to schedule this following command line instruction in Crontab:

speedtest-cli --csv &>> /home/pi/speedtestLog.txt

I am trying doing it:

*/1 * * * * speedtest-cli --csv >> /home/pi/speedtestLog.txt 2>&1

But I am getting the error appended in the file:

/bin/sh: 1: speedtest-cli: not found

How do I resolve it please?

Marcelo Gazzola
  • 123
  • 1
  • 4

1 Answers1

0

Some of the comments above are correct but they don't explain why.

The PATH environment variable is a list of filesystem paths that are searched for executable binaries, which usually looks like:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

The PATH variable is appended by the SHELL you are using. In ubuntu, /bin/sh links to the default shell, which is dash for system boot scripts (i.e. cron). Dash is similar to bash but not identical. In terminals, Bash is used by default.

On Debian-based systems (i.e. Ubuntu), PATH is initialized by pam_env based on the content of /etc/environment:

$ cat /etc/environment 
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

However, the shells like Dash/Bash also source /etc/profile and ~/.profile depending on how they are invoked (interactive vs login shell), where these scripts can append the PATH environment variable.

With all that said, speedtest-cli has to be in a folder on the PATH or you have to specify it using a full path. If you want option 1, you can try

*/1 * * * * . /home/pi/.profile ; speedtest-cli --csv >> /home/pi/speedtestLog.txt 2>&1

N.B. Thanks to steeldriver for pointing out that "source .profile" is a bashism and the posix standard is ". .profile".

Iyad K
  • 384
  • @steeldriver I don't have any evidence other than my limited experience with crontab. At some point I had a similar problem and it was solved by adding the path to the executable in the crontab. Since then I always include the full path in a cron job. – user68186 Aug 18 '19 at 14:03
  • @steeldriver evidence for setting PATH? From the man crontab(5):
       On  the  Debian  GNU/Linux  system, cron supports the pam_env module, and loads the environment
       specified by /etc/environment and /etc/security/pam_env.conf.  It also reads locale information
       from  /etc/default/locale.   However,  the  PAM settings do NOT override the settings described
       above nor any settings in the crontab file itself. Note in particular that if you want  a  PATH
       other than "/usr/bin:/bin", you will need to set it in the crontab file
    
    – Iyad K Aug 18 '19 at 18:07