I have a bash script in which I use the ip r command. When I run the script in bash it works but when cron runs it it does not.

This is the line in my script:

ping -q -w 1 -c 1 `ip r | grep -m 1 default | cut -d ' ' -f 3` >/dev/null || exit

To test that the ip r command is the problem I added notify-send "TEST $(ip r)" in front of that line. When I execute the script in bash it sends the notification with TEST and the output of ip r but when run with cron the notification only contains TEST.

  • @KamilMaciorowski whereis ip gives ip: /usr/sbin/ip /usr/share/man/man8/ip.8.gz. How do I find out what $PATH it is? Running echo $PATH in bash prints /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/alex/.local/bin. – Alexander Dec 14 '18 at 11:01
  • @KamilMaciorowski You were right, it was a problem with $PATH. I now added ` eval "export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin :/usr/sbin:/home/alex/.local/bin"` at the beginning of my crontab -e command and now the ip r works in cron as well. – Alexander Dec 14 '18 at 11:08

The $PATH variable was not set when run with cron. So I ran echo $PATH in bash and then set the $PATH variable to that output in the crontab command.

For that I added

eval "export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin :/usr/sbin:/home/alex/.local/bin"

in crontab -e where everything after PATH= is the output of the command echo $PATH when run in bash.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.