-1

I basically need to automate running 2 commands in separate terminals.

while :
do
    timeout 10 gnome-terminal --geometry=95x56 -e "COMMAND1" &&
    timeout 7 gnome-terminal -e "COMMAND2" &&
    sleep 30
done

Expected behavior:

  • A terminal opens, running COMMAND1 for 10 seconds, then closes
  • A second terminal opens, runs COMMAND2 for 7 seconds, then closes
  • 30 seconds pass
  • The cycle repeats

Actual behavior:

  • Both COMMAND1 and COMMAND2 start at the same time
  • COMMAND1 is displayed in the terminal, but does not actually run.

What's going on here?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
techtox
  • 1
  • 1
  • And the question is...? – Poshi Apr 13 '19 at 15:09
  • why run the command in seperate terminals? – Nidhoegger Apr 13 '19 at 15:14
  • The problem is that they start at the same time instead of waiting 10 seconds before the second command runs. Also, my first terminal is not running correctly. It basically opens a terminal and keeps entering the command, not running it like it would when I manually enter it in a terminal. I want to run them in seperate terminals because I want to be able to check if I can keep the first one running. – techtox Apr 13 '19 at 15:20
  • Running things in separate terminals is usually something you want to avoid. Run them as background jobs with logs to a file and then maybe start separate terminals to monitor those log files if that's your thing. – tripleee Apr 13 '19 at 15:38
  • 1
    If they both run at the same time, that smells like `gnome-terminal` either self-detaches (forks a subprocess without a handle on the controlling TTY, configures that subprocess to ignore the HUP signal, and then exits the parent process and thus returns control to the calling shell) or just passes off control to an existing session (connecting via dbus or such), which is how `emacsclient` and similar tools also work. Preventing that behavior is not really a bash question, it's specifically about gnome-terminal. – Charles Duffy Apr 13 '19 at 15:46
  • Is `"COMMAND1"` actually a string which contains a command with parameters? Maybe try `timeout 10 gnome-terminal -- COMMAND1` (too lazy to copy the geometry option but you can add that back in if this works of course). – tripleee Apr 13 '19 at 15:46
  • you may want to try the nohup command in front of your statements, depending on how your are calling this, https://en.wikipedia.org/wiki/Nohup. – applecrusher Apr 13 '19 at 15:49
  • @applecrusher, that's the exact opposite of what the OP is trying to do here. The OP wants to *stop* their program from self-daemonizing. `nohup` is used to force a program that otherwise couldn't be backgrounded to do run detached (though there's no good reason to use it even for that purpose; bash itself can do everything `nohup` does built-in, via appropriate redirections and the use of the `disown` shell builtin). – Charles Duffy Apr 13 '19 at 15:49
  • @techtox, I've tried to reformat for a less conversational, more informative writing style -- please make sure the result is complete and accurate. – Charles Duffy Apr 13 '19 at 15:56
  • @CharlesDuffy Thanks, I'll use the formatting if I have another question in the future. I've solved it, I really know nothing about scripting so I made a few mistakes. I did it in a different way but it works now. Also thanks for the tip! – techtox Apr 13 '19 at 19:05
  • Go ahead and use the "Add an Answer" button to add your own answer, instead of editing what you did into the question. (Otherwise, I'll do so on your behalf, adding a "Community Wiki" answer so as not to take credit for your own discovery effort -- but if you add it under your own name, you'll get credit if it's upvoted) – Charles Duffy Apr 13 '19 at 19:58
  • (BTW, it's also not necessary to put explicit "edit markers" in, showing what changed between revisions -- anyone who wants to see the edit history can click on a link to view diffs between all prior versions; what we value more here is clarity to new readers who haven't necessarily been following the question's evolution). – Charles Duffy Apr 13 '19 at 20:01

1 Answers1

0

Self-answer moved from the question into a Community Wiki answer, per What is the appropriate action when the answer to a question is added to the question itself?:

The following, as intended, lets only the first command stay open, while running the second in a loop:

COMMAND1 &
while :
do
    sleep 15
    gnome-terminal -- timeout 7 COMMAND2 &&
    sleep 30
done
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441