LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-13-2014, 09:30 AM   #1
mithun119
LQ Newbie
 
Registered: Aug 2014
Posts: 11

Rep: Reputation: Disabled
Run a script in background


I have a script which monitors the file passwd for change, running in the background initially. This will kickoff when a session starts, executed from the .profile.

When the passwd file is modified, it will interrupt the session and prompt the user for reason of change. After entering the reason the script now starts running in the foreground, but I want it to continue running in the background doing its job of monitoring.

Is there any way we can send it to the background from within the script.

Any help on this is much appreciated.
 
Old 10-13-2014, 09:42 AM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 805

Rep: Reputation: 186Reputation: 186
Yeh. Kill the old version and initiate it afresh.

Or - making a virtue out of necessity - log the user out and get them to log in again (thus executing whats in (dot)profile.

OK
 
Old 10-13-2014, 11:19 AM   #3
mithun119
LQ Newbie
 
Registered: Aug 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
That could be a work around, but not sure what went wrong, i am having an issue now.
The main script when notices change, calls another script which should then go into interactive mode, asking the user for reason. But not, when we enter the reason, the whole thing just stops.

Execution below:

[root@vmtest02 lab]# ./watchsession.sh passwd&
[1] 4131

=========> when changes made to passwd

[root@vmtest02 lab]#
Enter the ticket number or reason: Test
-bash: Test: command not found

[1]+ Stopped ./watchsession.sh passwd



Code snippet of the main script watchsession.sh and the called script bkp_and_cmd.sh :
--------------
*****************
Watchsession.sh
*****************

#!/bin/bash
export path=$1
:

:
:

update_sha() {
sha=`ls -ld $path | sha1sum`
}

update_sha
previous_sha=$sha


change() {

./bkp_and_cmd.sh ####This script will take question the user for reason, take backup of the file and return.

echo -en "\n--> resumed watching."
}

compare() {
update_sha
if [[ $sha != $previous_sha ]] ; then
echo -n "change detected,"
change
previous_sha=$sha
fi
}


while true; do
compare
sleep 1
done

****************
bkp_and_cmd.sh
****************

#!/bin/bash


DATE=`/bin/date +%Y%m%d-%H%M%S`
echo -n "Enter the ticket number or reason: "

FIM_reason() {

while read ticket && [ -z "$ticket" ]; do
echo "FIM Alert: You have to enter a valid ticket or reason for the change"
done
reason=`echo $ticket|sed 's/ *//g'`
echo "executing bkup"
BKUP
}

BKUP() {
if [ -z "$reason" ]; then
WARN
else
<commands to take file backup>

exit 99
fi

}



WARN() {
echo "You cant do that"
echo "You have to enter a valid ticket or reason for the change"
FIM_reason
}

trap WARN SIGINT
trap WARN SIGTSTP
:
:
FIM_reason

Last edited by mithun119; 10-13-2014 at 11:21 AM.
 
Old 10-13-2014, 11:24 AM   #4
mithun119
LQ Newbie
 
Registered: Aug 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
I put checkpoint echo commands and found that the script enters bkp_and_cmd.sh calls the function FIM_reason and exits just before entering the while loop.
It doesnt read the reason into the variable ticket.
 
Old 10-13-2014, 11:11 PM   #5
mithun119
LQ Newbie
 
Registered: Aug 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Anyone has any idea on what the issue could be here
 
Old 10-14-2014, 04:56 AM   #6
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 80
where was a reason got read in your script. your prompt for a reason nut never read it again you are checking for an unread reason.
NO stderr re-direction so whatever error it will throw goes to foreground

here is what i suggest:

Declare functions on top.
Do the backup first as you detect a change
read as you prompt, perhaps 'read' is what you need
Use proper redirections stderr, stdin, stdout

Last edited by SAbhi; 10-14-2014 at 04:57 AM.
 
Old 10-14-2014, 10:36 AM   #7
mithun119
LQ Newbie
 
Registered: Aug 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Still the same. I tried reading the reason first and then entering into the while loop, still the result is the same.
When prompted for the reason, the reason that is entered is not read into the variable but is hitting the terminal as command and the running process gets stopped.
If we do %% and bring it to the foreground, then it proceeds as desired. I still cant catch the bug or the programming error that I might not be aware of.

[root@vmtest02 lab]# ./watchsession.sh aa &
[1] 5253
[root@vmtest02 lab]# change detected,Changes not written yet
Backing up...

Enter the ticket number or reason:
testing
-bash: testing: command not found

[1]+ Stopped ./watchsession.sh aa
[root@vmtest02 lab]#
[root@vmtest02 lab]# %%
./watchsession.sh aa
testing
executing bkup
Backed up
Changes written now
The reason is testing

--> resumed watching.

Is there some way to toggle a script to foreground and background from within the script.
Like here, i want it to monitor a file for changes in the background, on sensing change come to foreground, prompt the user for reason, take backup and again go background and monitor for changes and the cycle continues.
 
Old 10-15-2014, 04:51 AM   #8
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 80
try to resolve errors first i suggest!!

how did you read the reason , while loop and other things i suggested?
try giving good details, please don't expect only one person to answer there could be others too watching the thread and can help you much better or earlier than me,only if they have something to check from your side.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i need to constantly run a script in the background anurupr Linux - Newbie 7 03-20-2010 05:15 PM
run script in background MikeQ Linux - Newbie 2 09-11-2009 07:21 PM
How to run a shell script in a background? anandv_1234 Linux - Newbie 3 12-23-2007 04:03 AM
how do i get a shell script to run in the background? tjyorkshire Programming 4 07-06-2007 03:28 AM
How to run a bash command in the background from perl script professorfrink Programming 3 11-13-2003 04:02 PM


All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration