5

My script (status.sh) is:

#!/bin/bash
SITE=http://www.example.org/

STATUS=$(/usr/bin/curl -s -o /dev/null -I -w "%{http_code}" $SITE)

if [ $STATUS -eq 200 ]
then
 echo $STATUS >> /home/myuser/mysite-up.log
else
 echo $STATUS >> /home/myuser/mysite-down.log
fi

I run:

$ chmod +x /home/myuser/status.sh

Then on my crontab i got:

* * * * * /home/myuser/status.sh

When I run:

$ /home/myuser/status.sh

The file /home/myuser/mysite-up.log contains:

200

But when cron run, the file /home/myuser/mysite-up.log contains:

000

What I am doing wrong?

EDIT: I modified the script adding:

set -x

as @Sobrique suggested and I the output is:

 SITE=http://www.example.org/
 /usr/bin/curl -s -o /dev/null -I -w '%{http_code}' http://www.example.org/
 STATUS=000
 '[' 000 -eq 200 ']'
 echo 000
rpayanm
  • 579
  • 1
    I'd suggest adding set -x to your script - it should generate output (and thus an email) of what's going on when it's running (including variable values). I can't see any environment variable or path differences which is the usual problem with cron. – Sobrique Aug 13 '15 at 16:25
  • So your script as posted tests for equality (-eq) whereas your script as executed appears to test for inequality (-ne) – steeldriver Aug 13 '15 at 17:55
  • @steeldriver you are right, I modified the script and for that the trace output is different, let me edit the question and fix it. – rpayanm Aug 13 '15 at 18:30
  • 1
    Different proxy settings? Is the environment variable http_proxy set in your normal environment? Is it set in your cron job? – Gilles 'SO- stop being evil' Aug 13 '15 at 23:18
  • Thank you @Gilles it was that! the proxy, if you want write this answer for I accept it. – rpayanm Aug 14 '15 at 12:44

2 Answers2

8

The main difference between running a script on the command line and running it from cron is the environment. If you get different behavior, check if that behavior might be due to environment variables. Cron jobs run with only a few variables set, and those not necessarily to the same value as in a logged-in session (in particular, PATH is often different).

If you want your environment variables to be set, either declare them in ~/.pam_environment (if your system supports it) or add . ~/.profile && at the beginning of the cron job (if you declare them in .profile). See also What's the best distro/shell-agnostic way to set environment variables?

In this case, a 000 status from curl indicates that it could not connect to the server. Usually, the network connection is system-wide, so networking behaves the same in cron. However one thing that's indicated by environment variables is any proxy use. If you need a proxy to connect to the web and you've set the environment variable http_proxy in a session startup script, that setting isn't applied in your cron job, which would explain the failure.

Add the option -S to you curl invocation to display error messages (while retaining -s to hide other messages).

0

It's possible curl used by cron might be different from what's used in the terminal/shell. Check which curl in the terminal (where it's working), and then either directly in cron or the shell script called by cron rather than just curl https://www.example.com/ use that full path: /home/myusername/anaconda3/bin/curl https://www.example.com/.

sbha
  • 101
  • 3