Operating System - HP-UX
1825094 Members
4859 Online
109679 Solutions
New Discussion юеВ

Re: Unable to include commands after calling nohup in shell script

 
SOLVED
Go to solution
Aji Thomas
Regular Advisor

Unable to include commands after calling nohup in shell script

Hi Guys,

I have a script to schedule backups. For this i am using
nohup sh scriptname &

and within scriptname i have an "at command" to schedule the backup.

The issue is within my backup script i have included as follows:

-----------------------------------------
nohup scriptname &
printf "\n\n\n\nScheduled backup at `at -l`"
------------------------------------------

Even though the backup is getting scheduled, when i try to use prinf to display the time it has been scheduled, it does not print.

Please advice how to invoke enter key since when you execute nohup command it requires an enter key to proceed to the prompt

Please advice how to incorporate this

AJi
24 REPLIES 24
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

Hi Aji ,

What error are u getting in nohup.out file,
Pls give some more info abt the script.

Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

Hi,

I am not getting any errors while issuing the nohup command, the issue is that the command next which does a printf of the `at -l` does not print, since nohup requires enter key to be pressed.

Thanks,
AJi
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

Hi Aji ,

Hope this will solve the problem,

make the lines like this :

-----------------------------------------
nohup scriptname &
echo "\n\n\n\nScheduled backup at `at -l`"
------------------------------------------

Just replace "printf" with "echo",

Hope this will work..

Cheers,
Raj.

" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

Hi Aji ,

You can use this also :
-----------------------------------------
nohup scriptname &
print "\n\n\n\nScheduled backup at `at -l`"
------------------------------------------

print will also work ..

Cheers ,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

Hi

Thanks for the advice,
But still its not working,
I tried echo as well as print command.
Its not showing the output of at -l in the next line after nohup.

Please advice
AJi
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

Hi Aji ,

Try giving , echo "\r" after the command to execute a Carriage Return.

Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

hi raj

"/r" too does'nt work for me

please advice me
AJi
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

HI

I mean echo "\r"

Regards
AJi
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

Seems some problem is going on , in between the two scripts ,

you can also try scheduling the backup through cron :

Example:

1.30 1 * * 0 /home/backup_script > /dev/null 2>&1

# at 1.30 Every Sunday.
---------------------------------------


You dont need nohup, for this.
And also wondering why you want to use nohup . Once you given the at command , the backup will be executed at the time , specified by at command , and it will not terminate even you logged out.
Nohup is useful if you are hangup the session , and not applicable with at and cron.

Hope it may help ,

Raj.
---
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

sorry , mistake in last posting.
the cron entry will be like this:

30 1 * * 0 /home/backup_script > /dev/null 2>&1

(to run the backup at every sunday 1.30)
------

Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

Hi

thanks for the update,

I am aware of the cron job. But this scheduling is not an daily activity, so we cannot schedule it as a cronjob. Similarly we are running a menu based script that will delete decide the backup policy, and accordingly it will delete older file and then schedule the at job. Finally it should print at -l to show the backup user that the schedule was successful.

This backup script i am executing with nohup, which causes the issue, that it does not print later.

Please advice me,
AJi
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

Hmm..

So wihout nohup , does the script prints output of at -l .

i.e If you run like this

sh scriptname &


Raj.
----
" If u think u can , If u think u cannot , - You are always Right . "
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

Hi

yes it works without nohup

But i want to execute within nohup. Please note my question. The issue is with issuing the at -l within nohup command

Please advice
AJi
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

Hi Aji ,

You have wrote the command in two way .

1 nohup sh scriptname &

2.
-----------------------------------------
nohup scriptname &
printf "\n\n\n\nScheduled backup at `at -l`"
------------------------------------------

The 2nd (nohup) command will run successfully , if you have made the script file executable . That is x flag is set.

Pls check,

Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

hi

the commad is been called as

nohup scriptname &
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

Hi Aji ,

I am wondeting , why other peoples are not replying to this thread .

May be you have not assigned any points to your queries:

" I have assigned points to 0 of 33 responses to my questions."

or May be today less poeples are online.

-----
Any way keep on working , the problem will get solve soon.

Cheers.
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

Hi

I too was wondering why others are not replying.
How can i assign point without getting a solution to my issue.

Regards
AJi
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

You are correct ,
But I am talking about the old issues.

----
Everything seems right in your script and it should give the output ,of `at -l` , as when it is printing the string "Scheduled backup" and the next is `at -l`.


Else you can do it like that ,

You can put this commands in the backup script , where the, at command runs .

---------------------------------------
..
..
at -f scname -t time # assuming this is your at command.
# add the below lines to get the notification .

echo " at Command executed ..at `date` "
echo " Now Notifying to the user.."
at -l > `tty`

####################################

Hope this will help,

Cheers ,
Raj.

" If u think u can , If u think u cannot , - You are always Right . "
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

Hi raj

The solution provided was GUd, but it does not work in my script

AJ1
Raj D.
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

Hi Aji ,

I understand that.

But now I am not getting anything furhter for that script problem, as not able to understand as whats wrong, with that. May some other people can give some gud solution.

thx n Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Stephen Keane
Honored Contributor
Solution

Re: Unable to include commands after calling nohup in shell script

You are running "scriptname" in the background and immediately asking for all scheduled jobs. What if the background script hasn't scheduled the backup before you run the "at -l" command, then "at" will return nothing. You'd just get the start of your message "Scheduled backup at".

Does the background job "scriptname" terminate at some point - could you not wait for it? Why is it run in the background in any case?

When does the background job actually run the "at" command to schedule the backup? Could you not get the background job to run the "at -l" and write the output to a file, then your foreground job could sit in a loop waiting for the file to be created, list its contents and remove the file?

Just my 10 cents worth.
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

Hi Stephen Keane,

Thanks for the update,

You were right,It worked.

I issued a sleep for 1 sec and then tried to print.

----------------------------------
nohup /home/orarac/start &
echo "\nScheduled backup at: `at -l`"
sleep 1
cat /home/orarac/nohup.out
------------------------------------


Thanks,
AJi
Muthukumar_5
Honored Contributor

Re: Unable to include commands after calling nohup in shell script

Try like.

#!/bin/ksh
#backup.ksh
nohup ./atscr.ksh &
printf "\n\n\n\nScheduled backup at `at -l`\n"

#!/bin/ksh
#atscr.ksh
at -f test.ksh now + 2 minutes

#!/bin/ksh
#test.ksh
ls
hostname
sleep 10
#end

Command line
# chmod u+x backup.ksh test.ksh atscr.ksh

# rm -f nohup.out
# ./backup.ksh
Sending output to nohup.out

Scheduled backup at user = root 1125916985.a Mon Sep 5 04:43:05 2005
user = root 1125917015.a Mon Sep 5 04:43:35 2005
# cat nohup.out
warning: commands will be executed using /usr/bin/sh
job 1125917056.a at Mon Sep 5 04:44:16 2005

Problem:

Change this nohup scriptname & to (chmod u+x scriptname)nohup ./scriptname &

hth.
Easy to suggest when don't know about the problem!
Aji Thomas
Regular Advisor

Re: Unable to include commands after calling nohup in shell script

I have got the solution from stephen

thanks guys for the support.

Regards,
AJi