Creating a bash script and I cannot get it to pause for a few seconds

Here is the script:

#!/bin/bash
xfce4-terminal -e "ping -c 4 google.co.uk"
sleep 10

Could somebody tell me what I am doing wrong? Thank you.

insert sleep in console command and not after (that the terminal is closed) :wink: use read is best


but why open terminal in bash script ? if is for gui, you can use a .desktop

you are running the command

xfce4-terminal -e "ping -c 4 google.co.uk"

This opens another shell in xfce4-terminal

sleep 10

is executed when you terminate xfce4-terminal

I'm not sure what it is you are trying to accomplish or why you wish to set up something like that, but I suppose the following would work:

#!/bin/sh      # You don't need to specify GNU Bash specifically;
               # rather just make it Bourne-compatible.

while true
do
     ping -c 4 google.co.uk
     sleep 10
done

Then save the file as a shell script, e.g. ~/bin/googleping and make sure it has execute permission.

chmod +x ~/bin/googleping

Then create a launcher that invokes...

xfce4-terminal -e "~/bin/googleping"

If the directory ~/bin does not exist, create it and add it to your $PATH ─ this is not strictly necessary for the above case, but it may come in handy later for storing other shell scripts.

Hope this helps. :wink:

2 Likes

I think because they want to execute the script in the terminal by clicking on it, which was the solution here:

@ocupi
I think you want to learn bash scripting, which is good, but I think it will be easier for you if you start from the terminal (using bash inside it).
Here are some tutorials two users suggested here:

[1] https://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html

[2] http://sourceforge.net/projects/linuxcommand/files/TLCL/19.01/TLCL-19.01.pdf/download

Maybe also worth to have a look at:

[3] https://en.wikibooks.org/wiki/Bash_Shell_Scripting

Based on a quick look, I think [2] suits you best. It starts with you using the bash prompt of a terminal.

Don't give up and have fun.

2 Likes

If this is the whole of your script, you can probably just do this:

#!/bin/bash
xfce4-terminal -e "ping -c 4 google.co.uk; sleep 10"

Alternatively, if you plan to continue to evolve this script you might consider breaking it into two pieces. For example:

launcher_script.sh
#!/bin/bash
xfce4-terminal -e "/path/to/run_script.sh"
run_script.sh
ping -c 4 google.co.uk
sleep 10

The fundamental issue is that the call to xfce4-terminal launches a terminal and runs the command that immediately follows the -e

Since this is shell scripting, there are also at least 50 other solutions.

3 Likes

Honestly, you are a lot better off telling us what you want to accomplish overall instead of telling what you have that doesn't work the way you want. The reason being if we know what you want to do, we can possibly give you multiple methods to accomplish the goal, including ways you may not have considered.

So for example whats your goal for pinging this site? Is it so you can manually look at the results? What do you then do with the results (does it help you make a choice, perform another action, record the data, etc.)?

I would consider redirecting the output to a file instead of pausing the script. This way the process/script completes faster, tying up less resources and being less prone to failure or getting stuck running forever. You also end up with results that last as long (or short) as you like in the form of a file you can open, close or modify as you like.

ping -c 4 google.co.uk > filename.txt

Or a variation of the above should direct the output to a file. >> would append results to the file instead of overwriting the previous results.

With more info on why you are trying to do this maybe we can provide better answers.

EDIT: to others helping. I am guessing the OP is doing:

xfce4-terminal -e "ping -c 4 google.co.uk"
vs
ping -c 4 google.co.uk

To open a terminal window in the GUI so they can see the results of the ping. As a few of you have mentioned, its essentially another shell/subshell hence why sleep is not working. In this case WHY seems more important to me than HOW.

1 Like

ok, bad idea to open 2 subjects.

create only a launcher ping.desktop (with chmod +x) and not a script bash !

[Desktop Entry]
Name=A ping 
Icon=web-browser
Terminal=false
Type=Application
Exec=xfce4-terminal -e "ping -c 4 google.co.uk && read"

with read : "return" key for close terminal (can replace by sleep)

4 Likes

When I run this in the terminal it works as expected. However on double click it does not open the terminal. File is set to run as program. Even right click > open with terminal does not work.

For all those who mentioned why do I want this script. I just want it as a fast test to confirm the internet connections is functioning. Say for example a website does not load, I could use this script to check the internet connection is up. Also I thought it would be a nice basic script just to get familiar with working with them. Thank you all for the comments.

This unfortunately does not bring up the terminal window. Thank you for your reply though.

How do I get it to run the sleep command in the same first terminal window?

When you run which in the terminal? If you are using the two file solution make sure they are both set executable.

I got things working with this:

xfce4-terminal -e "sh -c 'ping -c 4 google.com && sleep 10s'"

Thank you all for your help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

Forum kindly sponsored by