1828238 Members
2925 Online
109975 Solutions
New Discussion

Script hangs on error

 
Salman Ahmed Qureshi
Frequent Advisor

Script hangs on error

Hi,
I have a script which i execute. The command i execute in this scripts needs a user response "press any key", if it faces any problem and script hangs there. I want my script not to hang and continue untill the last command of script. Can anyone help me how to solve this?
Another thing is, on my one server i see a cron job as follows

/hotbackup.sh > g/hotbackup.log 2>&1

I want to know only the meangs of "2>&1" at the end of this command.

Thanks
8 REPLIES 8
Dennis Handly
Acclaimed Contributor

Re: Script hangs on error

>The command I execute in this scripts needs a user response "press any key"

You can redirect stdin to /dev/null for that step. Or use yes(1):
yes | command ...

>/hotbackup.sh > g/hotbackup.log 2>&1
>I want to know only the meaning of "2>&1"

This says redirect stdout to that file, then also redirect stderr to the (new) value of stdout.
You had the equivalent to: 1> file 2>&1
Salman Ahmed Qureshi
Frequent Advisor

Re: Script hangs on error

Hi,
Thanks for the help.
Actually the command which fails also returns an output which i want to see and thats why i can't use > /dev/null. So i also want to see the output of command and i also doesn't want it to wait for the input. I cam calling it as follows.

./system_backup.sh > /scripts/sys_backup_out.log

and sys_backup_out.log is my output file
Salman Ahmed Qureshi
Frequent Advisor

Re: Script hangs on error

Hi,
let me explain a bot more, my this scrit runs some backup command on tape, if tape is not present, it hangs and asks to press any key. Even if i use "yes" as yuo told me in previous post, it again asks same question untell tape is ready. My intention is that if tape is not ready, it should not hang there and should come to the bottom and at the bottom, send me an email what ever the output of script is so that by checking email i can know whether backup was successfull or not. If i see error message in the email, i would insert the tape to take the backup. But if during script run if tape is not there, script is waiting and waiting and waiting and not exitig.
Thanks
Thanks
Dennis Handly
Acclaimed Contributor

Re: Script hangs on error

>I can't use > /dev/null.

I said stdin but this doesn't solve your next request. < /dev/null

>it should not hang there and should come to the bottom and at the bottom, send me an email

This is a problem. It has to hang there. But you could have another process that looks at the log file and possibly ps(1) and checks for that "press any key" and send you mail.
And then you have to figure out how to press that key.
Salman Ahmed Qureshi
Frequent Advisor

Re: Script hangs on error

Hi Dennis
I didnt understand this

You had the equivalent to: 1> file 2>&1

In my example, where should i look for the errors. Can you please explan a bit more.

thanks in advance

Salman
Dennis Handly
Acclaimed Contributor

Re: Script hangs on error

>In my example, where should i look for the errors.

Basically you are redirecting both stdout AND stderr to g/hotbackup.log.
OldSchool
Honored Contributor

Re: Script hangs on error

"Even if i use "yes" as yuo told me in previous post, it again asks same question untell tape is ready"

that's going to be a problem, as it's not "hanging" there, its looping waiting for a response and a tape. my initial thought was to stick the process (with the error redirected) might allow you to see the error, if its no stuck in the buffer, but won't allow you to "answer" the prompt when you do get a tape inserted.

perhaps you could check the status of the tape drive with "mt -f /dev/rmt/???" before you execute whatever backup command you are running. if the tape isn't inserted / available, send your email and fail the remainder of the job.
Mark McDonald_2
Trusted Contributor

Re: Script hangs on error

Salman

When a command is run, it may need input and it will also have standard output (stdout) and error output (stderr)

"cmd > log" this will send standard output to log. If you add "2>&1" aswell, this will also send the error output to the log.

Hope this explains a little better?