Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Shell script 2nd command not executing

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
FeedMeAStrayCat
Posts: 79
Joined: 2014-09-05 15:28

Shell script 2nd command not executing

#1 Post by FeedMeAStrayCat »

Hi All,

I am building a very simple script to open a web browser to a website, then close the webbrowser some time later.

Code: Select all

#!/bin/bash

sleep 15
chromium "some web site
sleep 15
pkill chromium
The problem I am having is that the first and second command are executed just fine and the browser opens, but after that the sleep 15 command and the pkill command dont get executed. When I run the script it seems to pause after opening the web browser, if I press Ctrl C to cancel, the script seems to move on to the next command, but it goes no where. I tried testing this by placing an echo command like below. If I press Ctrl C to cancel it moves onto the echo command and then just hangs.

Code: Select all

#!/bin/bash

sleep 15
chromium "some web site
echo "sleeping now"
sleep 15
pkill chromium
Could this be an issue with pkill? When I run the commands in the terminal it seems to work just fine.

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: Shell script 2nd command not executing

#2 Post by tomazzi »

Shell script 2nd command not executing
FeedMeAStrayCat wrote: The problem I am having is that the first and second command are executed just fine
So, actually, it's the 3rd command which is "not executing". :roll:

Anyway, this is because the script is processed command by command, that is, each command has to be finished before next one is launched. Read about running commands/programs in the background (forking).
Odi profanum vulgus

FeedMeAStrayCat
Posts: 79
Joined: 2014-09-05 15:28

Re: Shell script 2nd command not executing

#3 Post by FeedMeAStrayCat »

Thank you for the reply tomazzi .

Now I know that i can put the process in the background by putting a & at the end of the line to make the command run in the background, works the same way at the command line.

I have read about Parent processes producing child processes and so on and forking a process. I am still trying to grasp it.

One thing that I cannot understand is what determines if a process is indeed complete? I understand that a bash script (or the command line) is read from top to bottom left to right. With other scripts I have written I have not had to put a command into the background. The script just continues after running the command.

Lets say for example using the echo command. If I write a script like,

Code: Select all

echo "Hello"
and then run the script, it displays "Hello" via the stdout (the terminal) and then returns the prompt. Why does this not happen when running

Code: Select all

chromium "some web site"
After the browser is opened and the web page loaded wouldn't that be the completion of the command?

User avatar
ammar
Posts: 14
Joined: 2011-12-16 12:18

Re: Shell script 2nd command not executing

#4 Post by ammar »

To fork a bash command to run in the background you add "&" to it.

Example:

Code: Select all

#!/bin/bash
sleep 15
chromium "some web site " &
sleep 15      # this sleep should be long enough for chromium to be fully starting before killing it
pkill chromium

FeedMeAStrayCat
Posts: 79
Joined: 2014-09-05 15:28

Re: Shell script 2nd command not executing

#5 Post by FeedMeAStrayCat »

wizard10000 wrote:
FeedMeAStrayCat wrote:...After the browser is opened and the web page loaded wouldn't that be the completion of the command?
Nope - as chromium is still running. Completion of the command would require closing the browser :)
Thanks bud that makes sense.

FeedMeAStrayCat
Posts: 79
Joined: 2014-09-05 15:28

Re: Shell script 2nd command not executing

#6 Post by FeedMeAStrayCat »

Ok so I got the command to run in the background per the "&".

Whats strange though is that I can kill chromium from the script, but when attempting to reopen it throws me an error message and wont open. I can open it fine from the desktop.

Iv'e done some research and it seems that it has something to do with the DISPLAY variable? But its a little over my head.

I tried adding the following to the script,

Code: Select all

export DISPLAY=":0"
But still a no go. Will keep looking.

Post Reply