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
# 8  
Old 04-13-2013
Quote:
Originally Posted by misterx12345
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.
It seems to me that putting the output from all of the cmd* scripts in a single file is just making your life harder (since you have to manually find the end of each command file's output and write it to a separate file to mail it back to the user), but I have done as you requested. I did sneak in two status lines in the output.txt file that should make it easier to separate the output after all of the cmd* files have finished and have also included the exit code in the closing separator line in output.txt since I would think your users will want that information.

This script logs when a script starts, when it ends (and includes its exit status), and when the last script ends to the file log.txt. Both log.txt and output.txt are cleared when you start this script; so don't run it a second time before you extract all of the data you need from output.txt. This seems dangerous to me, but it is what you requested.

Code:
#!/bin/bash
# Throw away output from previous runs.
> output.txt

# Redirect this script's output to log file.
exec > log.txt 2>&1

# Loop through all available cmd scripts.
i=1
while [ -f cmd$i ]
do      date "+cmd$i starting @ %c"
        date "+================ cmd$i start ================ %c" >> output.txt
        sh cmd$i >> output.txt 2>&1
        ec=$?
        date "+============= cmd$i exit code $ec ============= %c" >> output.txt
        date "+cmd$i exit code $ec @ %c" >> log.txt
        i=$((i + 1))
done
date "+Scripts finished @ %c"

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 04-13-2013
Thank you very much sir !!!
I will try this when I get in the office on Thursday.

Have a nice weekend.

Quote:
Originally Posted by Don Cragun
It seems to me that putting the output from all of the cmd* scripts in a single file is just making your life harder (since you have to manually find the end of each command file's output and write it to a separate file to mail it back to the user), but I have done as you requested. I did sneak in two status lines in the output.txt file that should make it easier to separate the output after all of the cmd* files have finished and have also included the exit code in the closing separator line in output.txt since I would think your users will want that information.

This script logs when a script starts, when it ends (and includes its exit status), and when the last script ends to the file log.txt. Both log.txt and output.txt are cleared when you start this script; so don't run it a second time before you extract all of the data you need from output.txt. This seems dangerous to me, but it is what you requested.

Code:
#!/bin/bash
# Throw away output from previous runs.
> output.txt
 
# Redirect this script's output to log file.
exec > log.txt 2>&1
 
# Loop through all available cmd scripts.
i=1
while [ -f cmd$i ]
do      date "+cmd$i starting @ %c"
        date "+================ cmd$i start ================ %c" >> output.txt
        sh cmd$i >> output.txt 2>&1
        ec=$?
        date "+============= cmd$i exit code $ec ============= %c" >> output.txt
        date "+cmd$i exit code $ec @ %c" >> log.txt
        i=$((i + 1))
done
date "+Scripts finished @ %c"

# 10  
Old 04-18-2013
The script works great.
I am just trying to figure out the script.

"exec > log.txt 2>&1"
what is the function of exec , shows all output of everything being executed?
"do date "+cmd$i starting @ %c" "
how does the script know how to send this to the log.txt?
what is "ec=$? "

i=$((i + 1))
this tells the scripts to restart the commands but with the next cmd file?

Thx.
# 11  
Old 04-18-2013
Quote:
Originally Posted by misterx12345
The script works great.
I am just trying to figure out the script.

"exec > log.txt 2>&1"
what is the function of exec , shows all output of everything being executed?
The general format for the exec command:
Code:
    exec command [redirections]

overlays the current shell script with the command named by command and performs the input and/or output redirections specified by redirections for that command. When no command is specified, the exec command performs the specified redirections for the current script. So, in this case:
Code:
exec > log.txt 2>&1

no command is specified, so the standard output of the remaining commands in this script that do not otherwise redirect their standard output will be written to the file named log.txt and any data written to standard error by the remaining commands in this script that do not otherwise redirect their standard error output will be sent to the same file. Since the redirection to log.txt uses ">" (rather than ">>"), data in log.txt (if there is any) will be discarded before continuing with the following commands.
Quote:
Originally Posted by misterx12345
"do date "+cmd$i starting @ %c" "
how does the script know how to send this to the log.txt?
That is what the exec command above did.
Quote:
Originally Posted by misterx12345
what is "ec=$? "
The shell variable $? is the exit status of the previous command. It is being saved in the variable ec because I need to use the value in the next two commands.
Quote:
Originally Posted by misterx12345
i=$((i + 1))
this tells the scripts to restart the commands but with the next cmd file?

Thx.
This increments the value of the shell variable i as the last of the commands in the while loop:
Code:
while condition
do      commands
done

After incrementing i, [ -f cmd$i ] (the condition in this while loop) tests whether or not there is a regular file named cmdx where x is the current value of the shell variable i? If cmdx does exist, the commands in the loop are executed again with the updated value of i.
This User Gave Thanks to Don Cragun For This Post:
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