LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Run a script in background (https://www.linuxquestions.org/questions/linux-newbie-8/run-a-script-in-background-4175521961/)

mithun119 10-13-2014 08:30 AM

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.

AnanthaP 10-13-2014 08:42 AM

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

mithun119 10-13-2014 10:19 AM

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

mithun119 10-13-2014 10:24 AM

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.

mithun119 10-13-2014 10:11 PM

Anyone has any idea on what the issue could be here

SAbhi 10-14-2014 03:56 AM

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

mithun119 10-14-2014 09:36 AM

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.

SAbhi 10-15-2014 03:51 AM

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.


All times are GMT -5. The time now is 08:25 AM.