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
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
Related
There are 2 shell scripts, test.sh & execute.sh, I need to run both the shell scripts using single cron job. Once the test.sh has completed execution then I need to run execute.sh sequentially. execute.sh must not be triggered until test.sh has executed successfully. execute.sh takes one parameter i.e properties file /user/abc/config.properties. I need to run this every one hour recursively. How to do it?
If I understood you correctly, a cron job like this would do:
0 * * * * /path/to/test.sh && /path/to/execute.sh /user/abc/config.properties
I have a script written in csh which I want to run weekly.
I tried using cronjob for this but it seems like cronjob is trying to run my script in sh and hence it is not working properly.
What can be done to make sure that the scripts runs properly in cronjob or is there any other way to accomplish what I am trying to do without using cronjob ?
My cron job looks something like this :
0 0 * * 6 source ~/cron_job
~/cron_job looks something like :
#!/bin/csh
source ~/.cshrc;
source ~/test_setup;
source ~/start_test
Also note that running source ~/cron_job directly on terminal works as intended but cronjob is not working. I get following error :
/bin/sh: source ~/cron_job : No such file or directory
0 0 * * 6 source ~/cron_job
This tries to "include" or "source" the file in the current script; this is wrong for a number of reasons:
You are trying to source a csh script;
even if it would be a sh script, cron expects you to run a separate program , not source something in the current script (perhaps it will work, I never tried, but consider two scripts sourcing something which have the same variable or function names. Oops!)
The correct way would be:
0 0 * * 6 csh -f ~/cron_job
This starts csh; the -f is to prevent loading startup files, which may sometimes interfere with the running of the sript.
The most probable cause of that behaviour is that you have a bourne shell configured in your /etc/profile file.
cron(8) uses your /etc/profile data to select the shell to use for executing your crontab(??) jobs, so in case you want to use a different shell you can do it in a subprocess, not using source.
Another way is to switch to csh(1) and use it instead of sh(1).
I am using Linux Centos to schedule a job.
I have created a shell script file called Im_daily_loads.sh to run the job at 12:42PM everyday.
with the following comands:
#!/bin/sh
42 12 * * * cd $pdi; ./kitchen.sh -file="/opt/kff/software/pdi/5.0.1.A/data- integration/projects/IML/code/stg/IML_Load_Frm_SRC_To_PSA.kjb" -level=Basic > -logfile="/opt/kff/software/pdi/5.0.1.A/data-integration/projects/IML/log/iml_daily_loads.err.log"
Then loaded the file into crontab by using the issuing the following command crontab Im_daily_loads.sh, but my job is not running.
What would be the problem?
Why not just use
crontab -e
as the user you plan to execute the job as, enter the job, save and exit the editor?
Also, it looks like you need to define $pdi in your script. How is crontab supposed to know where your script is located?
first , run a very simple job to be shure crontab works at all.
for example
set > /tmp/crontab_works.log 2>&1
it will write down all variables. so you will see not all variables available in crontab
I made a shell script and registered to execute every 20 minutes.
Here is my crontab code.
*/20 * * * * sh /mypath/run_myprocess.sh &> /dev/pts/34
I editted code like this in order to see whether my process run correctly.
I get the result '/dev/pts/34' from tty command in terminal.
However, does anyone know how to use linux command results(in this case: /dev/pts/34)
in crontab? This is because I will use several terminal to run my tasks.
For example, in shell script, I can use linux command result in the form of $(command) such as
echo "$(date)"
directly.
Plus, if I type something on the terminal during process running with crontab, it actually gives result. For example,
Process is running........
ls
backup backup.sh Desktop Task_Folder shared_folder
[UserID] ~ #
So I guess cron jobs run correctly but in background.
Please help me to find out how can I bring cron jobs in foreground.
If you start a job on your console and background it you can then bring it to foreground. If the task is not yours or not started on your terminal then you can not.
I am really new to Linux and I apologize if this is rudimentary, but I have Google'd to find examples with no clarity and I am confused. (the question relates to a server running CentOs 6)
My questions are:
I am not sure what is the default directory that I should store a .sh file in so that a cron job can run it.
Is the syntax and sequence of my code in .sh file below correct?.
I have tested the TSQL and its fine.
#! SQL="DELETE FROM messages WHERE date < DATE_SUB(CURDATE(), INTERVAL 7 DAY)"
MYSQL_USER="root"
MYSQL_PASS="xxxxxx"
MYSQL_DB="mydb"
I understand that the cron should contain this to do it on a daily basis:
0 0 * * *
But I am just having some apprehension of how to put it all together so I don't screw things up. A full example or explanation or a reference link would be greatly appreciated.
I believe that cron will execute the script from whichever directory it is in, given that:
the file has execution permission for the user that cron runs as (usually root if job is configured in the system-wide crontab)
the cron line specifies the full path to the script
So, if your script is /opt/script.sh, specifying this in cron:
0 0 * * * /opt/script.sh
will execute script.sh each day in 12:00am.
Please note that if this is the system-wide crontab (/etc/crontab) it should also include a username as which to execute the command:
0 0 * * * username /opt/script.sh
Also, something to make sure when working with cron is to either use full paths when calling external commands from the script or to set up the PATH variable (either in the script itself or on the crontab file). This is needed because usually the environment in which cron jobs are run is pretty restricted.
Another thing to have in mind is that if any output is generated by a cron job this output is sent via mail to the user executing the cron. So to have some feedback from the script you have to either set up the system so that the mail message ends up in a mailbox which is read by a human being or the script sends all of it's output to a log file or syslog.