Register
It is currently Wed Dec 13, 2017 10:29 pm

Background process in function


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sun Oct 15, 2017 1:58 pm   

Joined: Sat Apr 04, 2015 5:42 pm
Posts: 3
Hi!

This is only a simplified example of what I'm trying to do. The code below works like expected: A dialogue opens and after 3 seconds it's killed by the script.
Code:
#!/bin/bash

zenity --info --text="Info for you" &
ID=$!
printf "Process ID: %d\n" ${ID}
sleep 3
kill ${ID}
printf "%s\n" "Process was killed."

Next attempt is to put the dialogue part in a function, like this:
Code:
#!/bin/bash

MyFunction () {
   local x="$1"

   zenity --info --text="${x}" &
   printf "%d\n" $!
}

ID=$(MyFunction "Info for you")
printf "Process ID: %d\n" ${ID}
sleep 3
kill ${ID}
printf "%s\n" "Process was killed."

However, this doesn't work at all. The dialogue opens and nothing more happens, until the user manually close the dialogue. What's going on? How can I fix it?


Top
 Profile  
 PostPosted: Mon Oct 16, 2017 11:06 am   

Joined: Sat Apr 04, 2015 5:42 pm
Posts: 3
This forum seems very, very dead, or maybe the simple answer is that this is not possible.
However, I found that the following workaround is acceptable:

MyFunction.sh
Code:
x="$1"

zenity --info --text="${x}" &

MyScript.sh
Code:
#!/bin/bash

source MyFunction.sh "Some info to display."
ID=$!
printf "Process ID: %d\n" ${ID}
sleep 3
kill ${ID}
printf "%s\n" "Process was killed."

Testrun:
Code:
$ ./MyScript.sh
Process ID: 5886
Process was killed.
$

The dialogue popped up and the text was what I expected. Three seconds later the dialogue was killed, just what I expected.
One thing that I don't like with this approach, is that the variable in MyFunction.sh can't be local, so I need to make sure its name isn't the same as anything else.

Everyone's still very welcome with other suggestions. B-)


Top
 Profile  
 PostPosted: Fri Oct 20, 2017 11:06 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
I can't get, what you're going to achieve.
Running a process in the background id not intended to be "interactive"

Why don't you just output the message?


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP