BASH log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH log file
# 1  
Old 05-31-2013
BASH log file

hi .. am new to bash script..

Code:
#!/bin/bash

logfile = "${HOME}/log/test.log"

./1.sh

echo " script over "

can anyone telme hw to set the logfile ?? the above code calls the 1.sh and echo statement gets printed in cmd prompt, but i could not see the log file .
# 2  
Old 05-31-2013
There should not be any blank space around assignment operator:
Code:
logfile="${HOME}/log/test.log"

# 3  
Old 05-31-2013
That request is highly ambiguous. What do you mean by "set the logfile" and " i could not see the log file"?
What you do in your code snippet is assign an arbitrary text (that admittedly looks like a valid file spec) to a shell variable called John, Doe, or, in this case, logfile. (Actually, as Yoda points out, you don't even assign but asked the shell to run a command called "logfile" with parameters "=" and that text. Definitely, no log file is created, so you can't see it.
Try redirection of stdout and/or stderr to collect all output of your script into a file.
To create an execution log, set the -x option when running your script.
# 4  
Old 06-01-2013
Thanks yoda and rudic. i got it cleared .

Code:
#!/bin/bash



./bin/a.sh &

pid=$!

wait $pid

sft=$?

echo "a.sh job status : " $sft 1>>close.log 2>&1 

if [ $sft -eq 0 ]
then
	echo "a.sh is success at `date` " 1>>close.log 2>&1
else
	echo "a.sh failed at `date` " 1>>close.log 2>&1
	exit 1
fi



./bin/b.sh &

pid=$!

wait $pid

pt=$?

echo "b.sh job status : " $pt 1>>close.log 2>&1 

if [ $pt -eq 0 ]
then
	echo "b.sh is success at `date` " 1>>close.log 2>&1
else
	echo "b.sh failed at `date` " 1>>close.log 2>&1
	exit 1
fi

./bin/c.sh &

pid=$!

wait $pid

pit=$?

echo "c.sh job status : " $pit 1>>close.log 2>&1 

if [ $pit -eq 0 ]
then
	echo "c.sh is success at `date` " 1>>close.log 2>&1
else
	echo "c.sh failed at `date` " 1>>close.log 2>&1
	exit 1
fi

in the above code wen a.sh fails it should exit the script, but it goes on invoking b.sh .. can any bash folks pls tell me hw can i make the script to exit when a.sh fails . AM running a.sh and b.sh in background since they both are independent ..
# 5  
Old 06-01-2013
From what I see, the code should behave fine, i.e. exit if $sft=1. What was the output of the echo $sft?

Why do you run routines in the background and then do nothing but wait for them?
Why don't you put all those repeating, (almost) identical program parts into one function?
# 6  
Old 06-02-2013
am running a.sh and b.sh in background cos each script ll run longer. actually in the above code i made a mistake. i should enter and invoke c.sh only if both a.sh and b.sh are success.

i ran the above script and killed b.sh explicitly, but then the script ran and said b.sh completed successfully. My doubt is "$?" checks the success or failure of wait $pid .. correct ??

even when the script is killed wait $pid will have no process ID and $? will give 0 .. is that the way it works ??

to be precise my doubt is der anyway 2 check the success or failure of a child script running in background ??
# 7  
Old 06-02-2013
Quote:
Originally Posted by Rahul619
am running a.sh and b.sh in background cos each script ll run longer. actually in the above code i made a mistake. i should enter and invoke c.sh only if both a.sh and b.sh are success.

i ran the above script and killed b.sh explicitly, but then the script ran and said b.sh completed successfully. My doubt is "$?" checks the success or failure of wait $pid .. correct ??

even when the script is killed wait $pid will have no process ID and $? will give 0 .. is that the way it works ??

to be precise my doubt is der anyway 2 check the success or failure of a child script running in background ??
No. The exit status of wait (when given one or more pid operands) will be the exit status of the process with the PID matching the last operand given to wait. However, if you invoke wait with no operands, it will wait until all unwaited for children have terminated and then return exit status 0.

Running a.sh and b.sh asynchronously just makes your script more complicated if you wait for a.sh to finish before you start b.sh. An example showing how to run them in parallel is included below.

You haven't shown us what is in a.sh, b.sh, or c.sh. If they have trap handlers that catch an abnormal termination and later exit with a 0 exit status, you won't be able to detect that the child was killed (unless you use kill -9 or some other signal the child isn't catching to terminate it).

When I tried running your script, I created a.sh, b.sh, and c.sh that just ran an echo that reported that the script had started and gave its PID, and then slept for 15 to 30 seconds (depending on which script it was). I was able to retrieve non-zero exit status from any of them if I killed them from another window before the sleep terminated normally. (If you start your script synchronously and try to kill the children while the script is running, the kill won't happen until your script completes.)

If you'd like to run a and b in parallel then wait for both of them to finish before starting c, you could try something like the following. It was written and tested using bash (since that is the shell you specified). It will also work with any other POSIX conforming shell (such as ksh) without modification. In fact this script should even work with an old Bourne shell.

Some of the NOTEs and comments in this script may help you:
Code:
#!/bin/bash
# NOTE: This script runs a.sh and b.sh at the same tiem and reports the exit
# status of both before terminating (if either one fails) or moving to to c.sh
# (if both succeed).
# Start a and b...
./bin/a.sh &
pida=$!         # save pid of a.sh
./bin/b.sh &
pidb=$!         # save pid of b.sh

# Reap a...
# NOTE: The following wait will not collect the exit status of b.sh even if
# it finishes first.
wait $pida
esa=$?          # save exit status of a.sh

# NOTE: Following line creates close.log rather than appending to it.  So,
# we get a clean log for each invocation of this script.
# NOTE: There is no need to redirect stderr from these echo commands; the
# only reason for echo to fail would be if there is an error writing to the
# log file (and redirecting that error message to a file that can't be written
# to means that we would get no notification of the error).
echo "a.sh job status : $esa" >close.log
if [ $esa -eq 0 ]
then
        echo "a.sh is success at $(date)" >>close.log
else
        echo "a.sh failed at $(date)" >>close.log
fi

# Reap b...
wait $pidb
esb=$?          # save exit status of b.sh
echo "b.sh job status : $esb" >>close.log
if [ $esb -eq 0 ]
then
        echo "b.sh is success at $(date)" >>close.log
else
        echo "b.sh failed at $(date)" >>close.log
fi

# Exit if a or b failed...
if [ $esa -ne 0 ] || [ $esb -ne 0 ]
then    exit 1
fi

# Run c...
# NOTE: We don't have anything else to do while c.sh is running, so there is
# no need to start it asynchronously.
./bin/c.sh
esc=$?          # save exit status of c.sh
echo "c.sh job status : $esc" >>close.log

if [ $esc -eq 0 ]
then
        echo "c.sh is success at $(date)" >>close.log
else
        echo "c.sh failed at $(date)" >>close.log
        exit 2
fi

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

Call one bash file from another bash file.

hi .. greetings ...i need help in bash programming. i am on linux mint. i have two .sh files.(netcheck.sh and dlinkreboot.sh).. please note . both bash files are working perfectly well independently... but cant work together. . in short i want one bash file to call the other bash file... (2 Replies)
Discussion started by: emmfranklin
2 Replies

2. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Bash script for looking in log file

Hello, I'm a beginner in shell scripting. I would really appreciate some help from the forum. I want to write a small script that will look in apache error log. If it finds the appropriate word. It would execute some commands. In my case the apache error log is situated in:... (2 Replies)
Discussion started by: ajaysingh99
2 Replies

4. Shell Programming and Scripting

Bash Script Closest Timestamp (LOG FILE)

Hi All, I need something where I could take the date from FILE1 and in this case the date is Thu Sep 10 13:48:42 EDT 2015 and I need it to match the closest PREVIOUS date in the log file FILE2 this means in this specific case it would be Thu Sep 10 2015 13:35:28. I only need the closest... (3 Replies)
Discussion started by: roberto999
3 Replies

5. Shell Programming and Scripting

Log Out SSH User in Bash

So, I've been writing a system to allow users temporary access onto a system. Essentially, there's a web server with a PHP script, the PHP script takes a Username & Password from a webform, and passes it to a script, createusr.sh. The script looks something like this: pass=$(perl -e 'print... (2 Replies)
Discussion started by: FreddoT
2 Replies

6. Shell Programming and Scripting

how to make my own file as a running log file in bash

Hi, I have written a small script from that iam appending the output to a file.If multiple users invoke the same script or if i invoke the same script n number of times (using &), the output file(ZZ/OUT) contains messup information. #!/bin/bash # echo "Hello" >> /tmp/ZZ/OUT sleep 10 echo... (4 Replies)
Discussion started by: blrguest
4 Replies

7. Shell Programming and Scripting

bunch of ^@^@^@^@^@^@^@^@'s in bash log file

I have a bash script that has been running fine for months that scans a bunch of files and gives me a log file output, it has suddenly started putting 1.5M of a repeating sequence of ^@^@^@^@^@^@^@^@^@^@ on the first line of the logfile, is this a unicode problem I should be setting something in my... (5 Replies)
Discussion started by: unclecameron
5 Replies

8. Shell Programming and Scripting

Logging ALL standard out of a bash script to a log file, but still show on screen

Is it possible to store all standard-out of a bash script and the binaries it calls in a log file AND still display the stdout on screen? I know this is possible to store ALL stdout/stderr of a script to a single log file like: exec 1>&${logFile} exec 2>&1 But running a script with the... (3 Replies)
Discussion started by: ckmehta
3 Replies

9. Shell Programming and Scripting

bash: color strings in log file

hello im looking for an easy way to color specific strings in text file. simple example: cat file1 acb 1234 qwer 5678 my goal: cat file1 (color the acb red and the 5678 blue) acb 1234 qwer 5678 cheers (2 Replies)
Discussion started by: modcan
2 Replies

10. Shell Programming and Scripting

Bash tail monitor log file

Hi there, I have a problem here that involves bash script since I was noob in that field. Recently, I have to monitor data involve in logs so I just run command tail -f for the monitoring. The logs was generate every hour so I need to quickly change my logs every time the new hour hits according... (2 Replies)
Discussion started by: kriezo
2 Replies
Login or Register to Ask a Question