Script to run command one by one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to run command one by one
# 1  
Old 04-13-2013
Script to run command one by one

Hi,

I have run a lot of commands one after one,
but I can't run them simultaneous.
One command has to run and when finished second command has to run etc.
Also I would like to save all the outputs created by the commands in a file.
The commands can sometimes take hours, so it is also necessary to run them in the background or the whole script.
I am running bash shell.
The commands are very long, so I put them in these files: cmd1, cdm2, cmd3 etc
SO what I need is that the scripts runs and executes cmd1 and when finished , execute cmd2 etc.
This is what I got so far:

Code:
#! /bin/bash

# run commands in sequence

echo "Running cmd1"
sh cmd1 > output.txt 2>&1

wait

echo "Running cmd2"
sh cmd2 >> output.txt 2>&1


wait

echo "Running cmd3"
sh cmd3 >> output.txt 2>&1


echo " Scripts are finished !!"

Is it possible that the script itself fills in the number for the cmd files?
for example it will start with cmd1 and then cmd2 etc and stop until it can't find more?

Last edited by Scrutinizer; 04-13-2013 at 01:00 PM.. Reason: code tags
# 2  
Old 04-13-2013
Is this a homework assignment?
# 3  
Old 04-13-2013
This is for production environment.

Maybe this is better, but how would this run all the commands :

Code:
#! /bin/bash

for i in {1..30} ;

do

echo "##########Running cmd$i ##########$(date)"  > output.txt
sh cmd$i >> output.txt 2>&1


Last edited by Scrutinizer; 04-13-2013 at 01:01 PM.. Reason: code tags
# 4  
Old 04-13-2013
The script that you suggested will overwrite the output of all previously executed scripts with the output of the last script to run. The wait commands in your script don't do anything. (You didn't start the cmd* scripts asynchronously and it wouldn't make any difference if you did.) If you have some long running cmd* scripts, run this shell script in the background; not the jobs started by the script. You might want to try something like:
Code:
#!/bin/bash
i=1
while [ -f cmd$i ]
do      sh cmd$i > cmd$i_stdout.txt 2> cmd$i_stderr.txt
        i=$((i + 1))
done

Note that you specified that this is a bash script, but you're executing the cmd* scripts using sh (rather than bash). If the scripts to be run are executable scripts, you could omit the "sh" in red in the above script and let a #! line in each script specify the shell to be used for any script that doesn't want to use the system's default shell. This script will run any number of cmd* scripts as long as their names have consecutive integer values (starting with "1") at the ends of the names.

PS I didn't notice that you had used >> instead of > for later jobs, so you won't be overwriting the output. You may still want to consider the redirection methods shown above to separate stdout and stderr for each script into separate files. I also skipped the echoes you had showing the progression of scripts being executed. If you want them, I assume you know where to add them. (If not; let me know.)

Last edited by Don Cragun; 04-13-2013 at 12:26 PM..
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 04-13-2013
Thx Don for your quick reply.
Yes, I am using the bash, but should I use something else to run the commands?
I did run your script, but it didn't do anything.
Didn't get an error and no output files.
Seems like it didn't do anything.


Quote:
Originally Posted by Don Cragun
The script that you suggested will overwrite the output of all previously executed scripts with the output of the last script to run. The wait commands in your script don't do anything. (You didn't start the cmd* scripts asynchronously and it wouldn't make any difference if you did.) If you have some long running cmd* scripts, run this shell script in the background; not the jobs started by the script. You might want to try something like:
Code:
#!/bin/bash
i=1
while [ -f cmd$i ]
do      sh cmd$i > cmd$i_stdout.txt 2> cmd$i_stderr.txt
        i=$((i + 1))
done

Note that you specified that this is a bash script, but you're executing the cmd* scripts using sh (rather than bash). If the scripts to be run are executable scripts, you could omit the "sh" in red in the above script and let a #! line in each script specify the shell to be used for any script that doesn't want to use the system's default shell. This script will run any number of cmd* scripts as long as their names have consecutive integer values (starting with "1") at the ends of the names.

PS I didn't notice that you had used >> instead of > for later jobs, so you won't be overwriting the output. You may still want to consider the redirection methods shown above to separate stdout and stderr for each script into separate files. I also skipped the echoes you had showing the progression of scripts being executed. If you want them, I assume you know where to add them. (If not; let me know.)
---------- Post updated at 08:17 AM ---------- Previous update was at 07:33 AM ----------

Apologies, your command does work.
thx very much !!
but is it also possible to echo which command is going to run and date in front of every command in the log file?


Quote:
Originally Posted by Don Cragun
The script that you suggested will overwrite the output of all previously executed scripts with the output of the last script to run. The wait commands in your script don't do anything. (You didn't start the cmd* scripts asynchronously and it wouldn't make any difference if you did.) If you have some long running cmd* scripts, run this shell script in the background; not the jobs started by the script. You might want to try something like:
Code:
#!/bin/bash
i=1
while [ -f cmd$i ]
do      sh cmd$i > cmd$i_stdout.txt 2> cmd$i_stderr.txt
        i=$((i + 1))
done

Note that you specified that this is a bash script, but you're executing the cmd* scripts using sh (rather than bash). If the scripts to be run are executable scripts, you could omit the "sh" in red in the above script and let a #! line in each script specify the shell to be used for any script that doesn't want to use the system's default shell. This script will run any number of cmd* scripts as long as their names have consecutive integer values (starting with "1") at the ends of the names.

PS I didn't notice that you had used >> instead of > for later jobs, so you won't be overwriting the output. You may still want to consider the redirection methods shown above to separate stdout and stderr for each script into separate files. I also skipped the echoes you had showing the progression of scripts being executed. If you want them, I assume you know where to add them. (If not; let me know.)
# 6  
Old 04-13-2013
Quote:
Originally Posted by misterx12345
Thx Don for your quick reply.
Yes, I am using the bash, but should I use something else to run the commands?
I did run your script, but it didn't do anything.
Didn't get an error and no output files.
Seems like it didn't do anything.

---------- Post updated at 08:17 AM ---------- Previous update was at 07:33 AM ----------

Apologies, your command does work.
thx very much !!
but is it also possible to echo which command is going to run and date in front of every command in the log file?
I can't tell you what interpreter you should use to run your commands other than to say that the interpreter used has to be an interpreter that recognizes the commands it is being asked to run. You are the one who knows what is in the cmd* files. You are the one who knows if those files have a first line of the form #!interpreter. You are the one who knows if the cmd* files have been marked executable or are just readable. Without this information, I don't know what you should do. It just seemed strange that you were writing this script using bash, but using a Bourne shell to run the cmd* files. (Of course, you haven't said what system you're using; so sh and bash may be the same shell.)

Do you want all of the output from all of the cmd* files in one output file? Do you want the standard output from all of the cmd* files in one output file and standard error output from all of the cmd* files in a different output file? Or, do you want the standard output and standard error output for each cmd* file in separate output files (like the script I gave you currently does)?

Do you want the output showing which cmd* file is being started and the timestamp written to the same file as the standard output for the cmd* file that is about to run, to the standard output of the entire script, or to a different log file?
# 7  
Old 04-13-2013
I am using bash.
The reason for using the sh was something I found on the internet, wasn't aware it was bourne shell.
I just want to direct all the output to one file, which I did manage by replacing " > cmd$i_stdout.txt 2> cmd$i_stderr.txt " with " > output.txt 2>&1"
I just need now need the timestamp when every cmd* commands runs in a different file (this is to see how long the commands run).

we get these commands usually in one file from a different department, they don't have enough privileges on their account to execute these command
every command creates and *.csv file, we just copy the commands one by one into a file (commands are very long) and then execute it and send the *.csv file by email and
in the beginning we just got couple of commands, but now we are getting a lot and
these commands can sometime take hours to run
this is too much effort, because sometimes one command can take hours to run and we have to wait for the command to finish, before we can move on to the next one,
I want to automate this as much as possible and the script you gave does the job. We are working on Solaris 10 and I am using the bash shell.
Thx.

Quote:
Originally Posted by Don Cragun
I can't tell you what interpreter you should use to run your commands other than to say that the interpreter used has to be an interpreter that recognizes the commands it is being asked to run. You are the one who knows what is in the cmd* files. You are the one who knows if those files have a first line of the form #!interpreter. You are the one who knows if the cmd* files have been marked executable or are just readable. Without this information, I don't know what you should do. It just seemed strange that you were writing this script using bash, but using a Bourne shell to run the cmd* files. (Of course, you haven't said what system you're using; so sh and bash may be the same shell.)

Do you want all of the output from all of the cmd* files in one output file? Do you want the standard output from all of the cmd* files in one output file and standard error output from all of the cmd* files in a different output file? Or, do you want the standard output and standard error output for each cmd* file in separate output files (like the script I gave you currently does)?

Do you want the output showing which cmd* file is being started and the timestamp written to the same file as the standard output for the cmd* file that is about to run, to the standard output of the entire script, or to a different log file?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run script like command

hello i have write a script which can create username + password #!/bin/bash # Script to add a user to Linux system if ; then read -p "Enter username : " username read -s -p "Enter password : " password egrep "^$username" /etc/passwd >/dev/null if ; then... (3 Replies)
Discussion started by: nimafire
3 Replies

2. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

3. Shell Programming and Scripting

run command in a script shell

Hello, Please i'd like to run command in a script shell , how can i do ? here my commands : cd blcr-build // run command in this rep sudo insmod ./blcr_imports/kbuild/blcr_imports.ko //root sudo insmod ./cr_module/kbuild/blcr.ko //root Thank you. (1 Reply)
Discussion started by: chercheur857
1 Replies

4. Shell Programming and Scripting

Run command in background thru script

Dear All, Writing a script in which I want to run a command in background and keep it running even script is finished. I have tried like below, `truss -p <pid> >> & /tmp/log &` But doesnt work.. script goes running and nothing in log file. (7 Replies)
Discussion started by: Deei
7 Replies

5. Shell Programming and Scripting

script will not run cp command

Hi, Not sure what the issue is here, but when i run the script. A simple copy command, it does not find the cp command ? See scrpt below : #!/bin/sh set -x ############################################# # Backup Processes #... (4 Replies)
Discussion started by: venhart
4 Replies

6. UNIX for Dummies Questions & Answers

Script to run a command in a new terminal

Hey, I am trying to write a script that will open all of my session windows, and then secure shell into the appropriate server in the new windows. Seems simple, but I cant get it to work! Please help! :confused: (1 Reply)
Discussion started by: sojo1024
1 Replies

7. Shell Programming and Scripting

Getting script to run after ftp command

Hi I, essentially have two parts in my script. The first ftp's to server S10 and retrieves a batch of files. The second part does the crunching and arranging, They both work independently but when run all in sam script I cannoy get 2nd part to run, i.e. the cat, cut & sed. I think it may be... (10 Replies)
Discussion started by: rob171171
10 Replies

8. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

9. Shell Programming and Scripting

Why Does Command Run From Prompt But Not From Script?

I hope someone can shed any light on this mystery. I am trying to run the following command: su userID -c remsh server -l userid -n "awk -F^ '\$4 == \"SMITH\"' /tmp/infromational/version74b/LIVE/TEMPORARY/ABCfiles/HLC_Database_File.bat|head -1" > /tmp/variant/45BV32/var/store13.logfnd I... (15 Replies)
Discussion started by: Korn0474
15 Replies

10. Solaris

I want to run a script or command on other server

Hi all, I have done ssh-keygen to two servers in work place and given there entry for authorized_keys. I m able to ssh to other servers without asking password. But i face problem while trying to run a command or script on other server. It is throwing an Error. $ ssh... (4 Replies)
Discussion started by: naree
4 Replies
Login or Register to Ask a Question