Cronjob not working as intended

I have a shell script that I have scheduled using cron using a command:

0 10 * * * /directory/Script.sh > /directory/log/output.log

The script is scheduled to run at 10 AM everyday. The script executes but produces output files only with headers, no content is there.

The script produces two output files. When I run the script manually it works fine. But when scheduled it is not producing the correct output.

Help me out.

Thanks

Try this and see if you get some error in your output.log file.

0 10 * * * /directory/Script.sh > /directory/log/output.log 2>&1
1 Like

Okay, I wiil try this and can you please tell me how to schedule fr 5.10 AM as I have to wait for another half an hour to run this command.

Use this.

10 05 * * * /directory/Script.sh > /directory/log/output.log 2>&1

Read this to get the format for crontab
Cron format

I have tried your command but nothing is really working,

Yeah I checked out the output.txt, It shows

/EC2InstanceAuditScript.sh: line 20: ec2-describe-instances: command not found

How to resolve this?

---------- Post updated at 12:25 AM ---------- Previous update was at 12:16 AM ----------

Kindly see my edited reply

I think you have to provide the code for both scripts here.. so that we can know what cmds you are trying to execute

okay I will

That's correct, because your script not able to find the below cmd
ec2-describe-instances --> What is this cmd for?
I think you have to call it with full path... or you may have to define it in your script and call.

When I've come up with a different behaviour in script vs. cronjob execution, it has turned out to be probably because of one these (presuming you are in the same machine):

  • user executing the cron task is different from the one used for testing.
  • user is the same for cron and testing BUT it's using different environments.

Logging with "su" instead of "su -" it does not load the user's environment. So when running cron, the system executes the script loading the user's environment, of course. Then if testing when same user but different environment, there are many chances that the script is not going to behave in the same way or in the way expected.

Alternatevely, you may have logged in using a non-interactive shell like ssh to test the script, and then the script run using the local environment.

So how do you log in in the system when testing the script?

K.

Multiple reasons

1> Check full path of all executable in the script.

2> Ensure all environment variables are set accordingly

3> Check the script when run from the same user as the cron is executing.

Technically there is no difference between manually running a script and scheduling from cron

The script is working fine now using cron

Seem like you are not using the full command path. You must reference the "ec2-describe-instances" command to where you installed your ec2 cli like

/location/to/folder/ec2-describe-instances

Are you creating the crontab under the same user account as you have test the shell script manually?

  1. Always use the full path to the command in crontab
  2. You also always want to include the same environment variables as you use when you execute your shell script manually.

SetCron
setcron.com

command not found means an executable file for the command (ec2-describe-instances) was not found in any of the directories listed in PATH.

Common mistake. Job submitted by cron do not inherit any environment. It is the responsibility of the submitted script to guarantee all necessary environment variables are set. That's why it executes fine when you submit it from a command line. When you logged on, your session had a lot of environment variables (including PATH) set, mostly from your .bash_profile (or .profile). When you submitted the script from the command line, that process inherited your environment.

1 Like