LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pass Shell Script to Background Process (http://www.linuxquestions.org/questions/linux-newbie-8/pass-shell-script-to-background-process-910228/)

tg3793 10-26-2011 06:22 AM

Pass Shell Script to Background Process
 
Good morning. I'm trying to execute a shell script at the CLI and have it continue to run in the background even after the terminal closes.

This is what I've tried so far. Thanks;

Code:

sudo ./.ganttProj/ganttproject.sh && exit
sudo ./.ganttProj/ganttproject.sh &
bash -lic "sudo ./.ganttProj/ganttproject.sh & exit"

All three of them seem to get stuck at the end with GantProject doing a "Writing log to /home/scribe/.ganttproject.log"

The last one however has the advantage of GanttProject continuing to run after I manually do a ^C

Thanks for any help.

tronayne 10-26-2011 08:18 AM

Take a look at nohup.

In your shell program, make sure that you've used absolute path names for all commands and files; might help, can't hurt. You do not need to use exit with a program running in background, when it finishes the process will die (so, yeah, make sure that the process will actually finish and exit by running it in foreground). Oh, and when you launch it don't use the "./.balh," use absolute path name (avoids trouble). Finally, it's not a terribly good idea to run stuff with sudo; you may want to look into why you are running with sudo and what you can do so that is not needed.

Hope this helps some.

tg3793 11-03-2011 02:57 AM

Ok so here is a list of what I have tried so far and the results.

- - - - - - - - - - - - - - - - - - - - -

bash -lic "sudo ./.ganttProj/ganttproject.sh && exit"
Which works but leaves a CLI open; not passing the command in the background.

nohup bash -lic "sudo ./.ganttProj/ganttproject.sh"
popped open a CLI and then closed.

nohup sudo ./.ganttProj/ganttproject.sh
popped open a CLI and then closed.

nohup ./.ganttProj/ganttproject.sh
popped open a CLI and then closed.

bash -lic "sudo ./.ganttProj/ganttproject.sh &"
popped open a CLI and then closed.

tronayne 11-03-2011 07:09 AM

Why don't you try this:
Code:

chmod 755 ./.ganttProj/ganttproject.sh
nohup ${HOME}/.ganttProj/ganttproject.sh 2>&1 > /tmp/ganttproject.log &
OR
cd .ganttProj
nohup ./ganttproject.sh 2>&1 > /tmp/ganttproject.log &

That will launch your shell program in background, detach it from the terminal (so the terminal window can just sit there and you can type commands or whatever) and execute until it finishes.

You do not need to launch it with bash -cli blah to do this; the first line of your shell program should be
Code:

#!/bin/bash
You also do not need to (and should avoid doing) sudo to run this unless there is something you cannot read from or write to logged in as "you." If you cannot access something logged in as you, fix that or change the owner of the shell program to root and turn on the so-called su-bit so it'll run as root; i.e.,
Code:

su -
chmod 4755 ./.ganttProj/ganttproject.sh
^D OR exit
nohup ${HOME}/.ganttProj/ganttproject.sh 2>&1 > /tmp/ganttproject.log &

Monitor what's going on -- if your shell program produces messages -- with
Code:

tail -f /tmp/ganttproject.log
Hope this helps some.

tronayne 11-03-2011 12:42 PM

OK, I got to wondering what the heck you're trying to do so I went and downloaded and installed ganttproject-2.0.10.zip. It works just fine simply by opening a console window and entering
Code:

cd .ganttproject
sh ganttproject.sh

And, looking over your posts it looks like you may not have Java installed: You should be able to execute
Code:

whereis java
and see where java resides on your system (if it ain't there it ain't gonna work).

You could look in the log file that's created every time your run the program: ./.ganttproject.log; might give you a hint or two.

Frankly, there is no reason on this earth to execute that program in background, with sudo or any other variant of those -- it opens a screen, you can open the example provided with the download (HouseBuildingSample.gan).

Hope this helps some.

tg3793 11-04-2011 05:57 AM

Quote:

Originally Posted by tronayne (Post 4514735)
Why don't you try this:
Code:

chmod 755 ./.ganttProj/ganttproject.sh
nohup ${HOME}/.ganttProj/ganttproject.sh 2>&1 > /tmp/ganttproject.log &
OR
cd .ganttProj
nohup ./ganttproject.sh 2>&1 > /tmp/ganttproject.log &

That will launch your shell program in background, detach it from the terminal (so the terminal window can just sit there and you can type commands or whatever) and execute until it finishes.

tronayne; thanks so much for the feedback. I already checked the permissions in the GUI through nautilus and my username had read/write and execute access already. However just in case something was botched in the GUI I did the "chmod 755 ./.ganttProj/ganttproject.sh" as you had suggested.

Next here is the result of the next line I copied and pasted:
Code:

nohup ~/.ganttProj/ganttproject.sh 2>&1 > /tmp/ganttproject.log &
[1] 26469
scribe@scribe-desktop:~$ nohup: ignoring input and redirecting stderr to stdout

ganttproject.sh never seemed to run; or at least I never got the GUI that I had previously been getting.

I'll go through your other recommendations in a few moments.

tg3793 11-04-2011 06:12 AM

Quote:

Originally Posted by tronayne (Post 4514996)
And, looking over your posts it looks like you may not have Java installed: You should be able to execute
Code:

whereis java
and see where java resides on your system (if it ain't there it ain't gonna work).

Frankly, there is no reason on this earth to execute that program in background, with sudo or any other variant of those -- it opens a screen, you can open the example provided with the download (HouseBuildingSample.gan).

Hope this helps some.

Ok here is my java:

Code:

java: /usr/bin/java /etc/java /usr/share/java /usr/share/man/man1/java.1.gz
The reason for wanting to push the program into the background is because I'm trying to create a shortcut to it in the Gnome applications dropdown. And if I'm going to run it from there then why would I want to have both the GUI 'and' an extra terminal open :-)

I'll be happy to follow any instructions you give me. Thanks for your help.

Andrew Benton 11-04-2011 07:53 AM

If you want to run a program in the background after you've closed the terminal try using setsid. Eg:
Code:

sudo setsid ./.ganttProj/ganttproject.sh && exit

tronayne 11-04-2011 01:53 PM

One of the instructions at http://www.ganttproject.biz/download is:
Code:

Make sure that Sun's Java Runtime is installed on your Linux box and that you can run java from the command line. Unzip the file in your home directory ($HOME) and run $HOME/ganttproject-2.0.10/ganttproject.sh script.
Please note that GanttProject will not run with gij/gcj Java Runtime which comes by default with some Linux distributions. You do need either Sun's JRE or OpenJDK.

Note the bold face above: Do you in fact have Sun's Java Runtime (JRE) installed?

One way to tell is
Code:

java -showversion
Which should show you something similar to this:
Code:

java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)
<and lot's more stuff on down from there; note yours may not be 64-bit>

Hope this helps some.


All times are GMT -5. The time now is 10:05 PM.