I am executing one shell script with crontab which has to give more than 6000 lines output only it is giving output up to 2045 lines then coming out. when I run that shell script it is working fine giving more than 6000 lines

My script is not having any errors when run from bash. But when I run from crontab it is redirecting output up to only 2045 or 2046 lines.

my script file is with following line /script/scriptip.sh

awk '$2 ~ /10.169/ || $2 ~ /10.170/ {print $2}' /script/iplist.txt > /script/ipid.txt

my iplist.txt will have 18000 lines text

and my crontab entry is

 * * * * * /script/scriptip.sh

i run manually with

  • 1
    Can you post your shell script? – cuonglm Feb 25 '14 at 13:18
  • 1
    Is the output of the first 2045 when running from the shell the same as when running from crontab? – Zelda Feb 25 '14 at 13:40
  • 1
    There's probably an error occurring when started by cron. I suspect you have something established in your login environment that is absent in the cron environment. Or perhaps an error in your cwd assumption and file paths are incorrect. The output must differ at some point. run diff on both your outputs, it will be the first difference that is significant. – X Tian Feb 25 '14 at 13:42
  • 3
    Please, Don't test with a command that is different to crontab entry, ./script/scriptip.sh is ambiguous since it depends on your Working Directory which we can not see, and then need to ask yet another question. We are working in the dark here and only have the information you supply. – X Tian Feb 25 '14 at 15:06
  • 2
    With that crontab entry, it is going to truncate and rewrite /script/ipid.txt every minute. When you look at the file, is it perhaps in the process of being written to by the script? – Mark Plotnick Feb 25 '14 at 15:57

OK, I think the problem is the difference between absolute and relative paths. You have not given us much to go on but I am guessing that you have a directory in your $HOME called script and that is where you keep your scripts.

So, you open a terminal (this opens in your $HOME by default) and run


. means your current directory, so the command above actually runs


However, in your crontab, you need to use an absolute path since it will not be started from your $HOME directory. Try replacing your cron line with this one which uses the absolute path:

 * * * * * /home/kotesh/script/scriptip.sh

Also make sure that you are using absolute paths in the script itself:

#!/usr/bin/env bash
awk '$2 ~ /10.169/ || $2 ~ /10.170/ {print $2}' /home/kotesh/script/iplist.txt > /home/kotesh/script/ipid.txt
  • no my scripts are in / root folder not under /home/kotesh i did not told my script is not running with crontab. with crontab i am able to execute but instead of getting more than 6000 lines i am getting only 2045 lines in my output file – kotesh Feb 25 '14 at 15:35
  • @kotesh OK, and are you using absolute paths? You have ./script in your question and that is confusing us. – terdon Feb 25 '14 at 15:37
  • @kotesh also, please show the actual script, including the shebang line (the !#/bin/sh or equivalent) so we know how the script is being called. – terdon Feb 25 '14 at 15:39
  • when run the same script manually i am getting correct more than 6000 lines output. when scheduled crontab only i am getting only 2045 lines output – kotesh Feb 25 '14 at 15:44
  • #!/bin/bash also added first line – kotesh Feb 25 '14 at 15:45

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.