LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-22-2009, 03:59 AM   #1
srimal
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Rep: Reputation: 0
Question Runs Multiple shell script inside a main script using crontab


hi all,

I wrote a simple shell script called main.sh and inside it calling another shell script called rename.sh.Both are placed in a same directory.[/home/srimal/test]

$cat main.sh
Code:
#!/bin/sh
echo "Hello Buddy !!!!|Last Update : "`date --date="-1 days" +%Y-%m-%d`"|" >> Report.txt
sh rename.sh
exit
$cat rename.sh
Code:
#!/bin/sh
mv Report.txt "`date --date="-1 days" +%Y%m%d`""Report".txt
exit

What I want to do here is to Run main.sh in every day at 13.10pm
So I did as follows but file rename thing did not execute and it created in my home directory not in the test directory.

$crontab -u srimal -l
Code:
10 13 * * * /home/srimal/test/main.sh
However my idea is to learn how to run multiple shell scripts using crontab rather than only one shell script.Can any one guide me to a solution please.

Thanks.

Last edited by srimal; 10-22-2009 at 04:00 AM.
 
Old 10-22-2009, 04:09 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by srimal View Post
hi all,

I wrote a simple shell script called main.sh and inside it calling another shell script called rename.sh.Both are placed in a same directory.[/home/srimal/test]

$cat main.sh
Code:
#!/bin/sh
echo "Hello Buddy !!!!|Last Update : "`date --date="-1 days" +%Y-%m-%d`"|" >> Report.txt
sh rename.sh
exit
$cat rename.sh
Code:
#!/bin/sh
mv Report.txt "`date --date="-1 days" +%Y%m%d`""Report".txt
exit

What I want to do here is to Run main.sh in every day at 13.10pm
So I did as follows but file rename thing did not execute and it created in my home directory not in the test directory.

$crontab -u srimal -l
Code:
10 13 * * * /home/srimal/test/main.sh
However my idea is to learn how to run multiple shell scripts using crontab rather than only one shell script.Can any one guide me to a solution please.

Thanks.
You don't say in any detail what you expected, what you got, or how they differ. In scripts launched from cron, you must use full paths for everything -- executables, paths to files, everything. If you put a fill path before every command and every file reference, including the original cron call, you will get more reliable results.

In your "main.sh" script, you aren't really executing a different script as a separate process, because the original script must wait for the child to exit. Is this what you wanted?
 
Old 10-22-2009, 04:13 AM   #3
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
First, I would suggest you to use full path of commands when using cron even in your scripts.
Second Cron runs in your home directory, so once again full paths will help.

eg:
Quote:
#!/bin/sh
/usr/bin/echo "Hello Buddy !!!!|Last Update : "`date --date="-1 days" +%Y-%m-%d`"|" >> Report.txt
/usr/bin/sh /home/srimal/test/rename.sh
exit
Note find out the right place of these commands first (they may not always be in /usr/bin)
 
Old 10-22-2009, 05:36 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by vijay_babu1981 View Post
First, I would suggest you to use full path of commands when using cron even in your scripts.
Good advice. Small clarification: echo is a shell built-in so that does not need a full path (unless you don't want to use the shell built-in). The type command will confirm this
Code:
type echo
 
Old 10-22-2009, 06:19 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
All good advice; I also recommend
Code:
10 13 * * * /home/srimal/test/main.sh >/home/srimal/test/main.log 2>&1
Reading that log file would have pointed out some of your issues.
For really recalcitrant progs, add

set -xv

as the first line after the

#!/bin/bash

line

Last edited by chrism01; 10-22-2009 at 06:20 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script not working through crontab paragkalra *BSD 1 04-15-2009 12:36 PM
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
perl inside a shell script baidym Linux - Newbie 2 05-05-2008 06:42 PM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM


All times are GMT -5. The time now is 10:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration