My script (status.sh) is:


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

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

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:


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


What I am doing wrong?

EDIT: I modified the script adding:

set -x

as @Sobrique suggested and I the output is:

 /usr/bin/curl -s -o /dev/null -I -w '%{http_code}' http://www.example.org/
 '[' 000 -eq 200 ']'
 echo 000
  • 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) 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? 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

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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