0

It is necessary to run the bash script with cron from under the current user, but it is not executed when cron is running. I was looking at similar topics on the Internet, and I was unable to find out the reason. Please help me find the root of the problem.

admin@user-desktop:~$ crontab -e
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * sh /usr/bin/local/send_my_ip.sh >/dev/null 2>&1

May 15 20:31:01 user-desktop CRON[408639]: (admin) CMD (sh /usr/bin/local/send_my_ip.sh >/dev/null 2>&1)
May 15 20:37:01 user-desktop CRON[408805]: (admin) CMD (/bin/bash /usr/bin/local/send_my_ip.sh >/dev/null 2>&1)
May 15 20:38:01 user-desktop CRON[408841]: (admin) CMD (/bin/bash /usr/bin/local/send_my_ip.sh >/dev/null 2>&1)

admin@user-desktop:~$ ls -l  /usr/local/bin/send_my_ip.sh
-rwxrwxrwx 1 admin admin 569 may 15 20:27 /usr/local/bin/send_my_ip.sh
 
#!/bin/bash
. ~/.bashrc

MY_IP=$(wget -qO- eth0.me)
MY_IP_LAST=$(cat ~/my_ip.txt)
echo "$MY_IP" >> /home/admin/log_cron.log

don't work Shell script not running via crontab, but runs fine manually

shaman888
  • 283
  • 1
  • 10
  • Try to add: `. ~/.bashrc` after the shebang in your script. – Gilles Quénot May 15 '23 at 15:57
  • #!. ~/.bashrc it didn't help, but the script itself works – shaman888 May 15 '23 at 16:04
  • 1
    @shaman888 place the `. ~/.bashrc` on a ***separate line after*** the `#!/bin/bash` – markp-fuso May 15 '23 at 16:09
  • See https://stackoverflow.com/tags/cron/info for good info on debugging cron problems. I have added the `cron` tag as well to your Q. Good luck. – shellter May 15 '23 at 16:10
  • 2
    I'd have a concern with your crontab entry ... why are you calling the script with `sh` instead of `bash`? on your system is `sh` a copy of (or simlink to) `bash`? – markp-fuso May 15 '23 at 16:10
  • 1
    `sh /usr/bin/local/send_my_ip.sh >/dev/null 2>&1` The `/dev/null` part is currently discarding the output of your script. You should redirect the output of the script to a file so you can see the error message. – Nick ODell May 15 '23 at 16:12
  • Not related to the problem, but remember to quote all your variables. Paste your code into shellcheck.net to look for other problems. – Barmar May 15 '23 at 16:14
  • `[ $X_1 == '']` is no valid syntax. Please paste your script at [shellcheck.net](http://www.shellcheck.net/) and try to implement the recommendations made there. – Cyrus May 15 '23 at 16:20
  • admin@user-desktop:~$ sudo cat /var/log/syslog | grep CRONMay 15 21:20:01 user-desktop CRON[410048]: (admin) CMD (sh /usr/bin/local/send_my_ip.sh ) May 15 21:20:01 user-desktop CRON[410047]: (CRON) info (No MTA installed, discarding output) – shaman888 May 15 '23 at 16:20
  • `No MTA installed`: Install and configure a MTA (mail transfer server), e.g. `postfix`. – Cyrus May 15 '23 at 16:30
  • That means it generated some output but it could not send it to you because you don't have mail configured. Try writing the output to a file in a directory where you have write access, instead of to `/dev/null` – tripleee May 15 '23 at 16:31
  • @Cyrus I don't need cron to send me anything by email, for this I previously added it to the cron ">/dev/null 2>&1" – shaman888 May 15 '23 at 16:35
  • But that's why nobody can tell you what's wrong. We (and you) need to see the error message before this problem can be diagnosed. – tripleee May 15 '23 at 17:59
  • Typo: Surely `/usr/bin/local` should be `/usr/local/bin`? – tripleee May 15 '23 at 18:32
  • Why do you need `. ~/.bashrc` at all? What's in this file? – tripleee May 15 '23 at 18:34

0 Answers0