Run multiple shell scripts sequentially using single cronjob - linux

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

Related

Start bash script from bash script as cronjob

I'm trying to start a bash script(test.sh) from a second bash script that runs as a cronjob(startTest.sh) on Ubuntu 14.04.
Cron is running and both scripts work perfectly if called from command line.
startTest.sh looks like this:
#!bin/bash
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/home/username/path/to/script
bash /home/username/path/to/script/test.sh
test.sh looks like this:
#!/bin/bash
touch it_works.txt
My crontab entry looks like this
* * * * * /usr/local/bin/startTest.sh
Best practice is generally not to use relative paths (unless you do an explicit cd) in scripts run as cron jobs.
crond is probably not running from whatever directory you expect it to. Depending on what user this cron job runs as, the script either does not have permission to create it_works.txt in crond's current working directory, or it is creating the file and you're looking in the wrong place.

Cronjobs after each other

I have a lot of php-scripts which I would like to execute as cronjob. It´s important that those scripts are executed in the right order and are not running at the same time. How can I set up cronjobs which run after each other?
Each cron job runs when it's scheduled to run, regardless of whether any other cron jobs happen to be running.
Just make a cron job that executes several commands sequentially:
* * * * * command1; command2; command3
The command (in this case command1; command2; command3) is executed by /bin/sh (or by a shell you can specify by setting SHELL in your crontab). /bin/sh, or any shell worthy of the name, knows how to execute commands sequentially.
If there are a lot of commands you can put them into a shell script and execute that from cron.

Scheduling jobs in Linux using crontab

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

Scheduling a shell script in cron doesnot produces required output

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

CRON : Scheduling task

I am using ubuntu 12.04.I am using a script(./home/sam/code/imageUpdate) to synchronising images from server to a particular folder in local system. And I have to run the script in the evening always. So I want to write a crontab which will automatically runs the script.
My commands :
$crontab -e;
And added the scheduled time to the crontab file.
# 50 17 * * * cd /home/sam
# 52 17 * * * ./code/imageUpdate > image1.txt
Then I saved the file and waited for the result.
But I didn't get any result. No image was been synchronised to image1.txt file.
Have I left any step ?
Please help me out...
Thanks in advance.
Make sure you don't have hashes (comments) at the start of your crontab commands.
Additionally:
Crontab commands should be run in isolation.
Each crontab command will be run in its own context, changing directory in one instruction probably won't lead to that directory being sound for the next executed (they may be run in their own environments, e.g.).
To overcome this, write a simple shell script which encompasses all of your commands for a single action.
# MyCommand.sh
cd /home/sam
./code/imageUpdate > image1.txt
# crontab command
50 17 * * * /home/sam/MyCommand.sh

Resources