2

Based on feedback in comments tried this command: In a terminal window this works:

gnome-terminal -x bash -c "sudo ./serial"; xdotool getactivewindow windowminimize 

However when running the command in Startup Program the xdotool seems to be ignored. The pc boots with terminal windows on the screen, not minimized.

Background History:

I'm trying to determine a way to minimize the terminal window with the gnome-terminal command. I need my Ubuntu 14.04LTS remote pc to run several scripts and a C program at startup. This remote pc is for customer viewing so the monitor needs to be clean. I did not see an option available to minimize: http://manpages.ubuntu.com/manpages/wily/man1/gnome-terminal.1.html. The best I could do was to make the terminal windows small, which is acceptable if I don't find a better way. This is my command entered into Ubuntu's "Startup Applications" program:

gnome-terminal --geometry=60x2 --command "sudo ./My_C_Program"

My programming/Linux skills are mediocre, but growing. I'm looking for a better way to do this.

Based on a comment/feedback tried this:

gnome-terminal -x bash -c  "sudo ./serial_ver2_machine3_free_air_3_95"; wmctrl -r :ACTIVE: -b add,shaded

This works just the way I want when executing in a Terminal window!! The Terminal window is shaded but program is still running and it reappears when I click twice on Ubuntu's Terminal icon.

However when executing within "Startup Applications" the Terminal window is not 'shaded'. Using 'add,hidden' does not work. Using 'add,below' works, except I'm using my desktop background image as part of the customer viewing.

  • 1
    Run the command at login ? askubuntu.com/questions/270049/how-to-run-a-command-at-login – Panther Oct 4 '17 at 16:14
  • Maybe using wmctrl, like wmctrl -r :ACTIVE: -b add,hidden or wmctrl -r :ACTIVE: -b add,below? – dessert Oct 4 '17 at 16:33
  • 1
    Does this command need to be run inside a terminal? – glenn jackman Oct 4 '17 at 21:08
  • I doesn't need to run in terminal, but I remotely monitor with Teamviewer and access the terminal window and see/search print messages. I do export print messages to a log file, so I probably don't need it to run in terminal. – Quickseal Oct 6 '17 at 14:37
1

Another solution is to create tmux session at system startup and execute some commands inside. Then you will be able to attach to this session no matter whether within ssh or gnome-terminal. tmux has more advanced usage, here I will try to cover the most basic level.

1. Install tmux: sudo apt update && sudo apt install tmux

2. Create auxiliary executable script file. Let's assume it is placed in user's $HOME directory and it is called run-my-tmux-session:

touch "$HOME/run-my-tmux-session"
chmod +x "$HOME/run-my-tmux-session"

The script should look as this:

#!/bin/bash

SESS="my-tmux-session"                               # Set the session name

if (tmux has-session -t "$SESS" 2> /dev/null); then  # If session with name $SESS doesn't 
        echo "Session '$SESS' exists."               # exists
else                                                 # create it and send few commands
        tmux new-session -d -s $SESS
        tmux send-keys -t $SESS "echo 'Now:'; while true; do printf '%(%c)T\r'; done" ENTER
fi

exit 0

3. Now if you execute ~/run-my-tmux-session the script will create tmux session, called my-tmux-session and will execute few commands inside it. Here are few hints:

  • To list all sessions: tmux ls

  • To kill a session or to kill all sessions:

    tmux kill-session -t <session-name>
    tmux kill-session #
    
  • To attach to an existing session:

    tmux a -t <session-name>
    tmux attach -t <session-name>
    
  • To leave (detach) session run the follow command from within the session: tmux detach

  • To send a command to a session without attaching:

    tmux send -t <sess-name> "<commands>" ENTER
    tmux send-keys -t <sess-name> "<commands>" ENTER  
    

4.A. Launch the tmux session from Startup Applications. Open Startup Applications and create a new entry. Its command should be (replace <user> with the actual username):

/home/<user>/run-my-tmux-session

4.B. Launch the tmux session from crontab on reboot. Edit the user's crontab file and add the following lines to the bottom (type crontab -e to access user's crontab):

SHELL=/bin/bash
@reboot "$HOME/run-my-tmux-session"

4.C. Launch the tmux session from /etc/rc.local. This is probably the best solution! Edit the file /etc/rc.local and add one of the following lines before the last line in the file, that is exit 0 (replace <user> with the actual username):

su -l <user> -c "/home/<user>/run-my-tmux-session"
sudo -iu <user> "/home/<user>/run-my-tmux-session"

References:

Further reading:

  • 1
    I made the script as you described and used option 4.A and it started my c program and running in the background through tmux. tmux worth learning and utilizing. Solved! – Quickseal Oct 9 '17 at 16:51
1

Here is a workaround that uses xdotool: Create an auxiliary startup script, that will be executed within a gnome-terminal, which is opened by Startup Applications. The script will execute some commands and will minimize the active window. Please read carefully the comments within the script's body bellow.

1. Install xdotool: sudo apt update && sudo apt install xdotool

2. Create auxiliary executable script file. Let's assume it is placed in user's $HOME directory and it is called run-and-minimize:

touch "$HOME/run-and-minimize"
chmod +x "$HOME/run-and-minimize"

The script should look as this:

#!/bin/bash

# Put your commands here, for example:
echo 'The current time is:'

sleep 2                                            # Adjust this value. The mouse should not be moved while this period otherwise some other window could become active!
xdotool windowminimize $(xdotool getactivewindow)  # Minimize the window

# Or put your commands here, for example:
while true; do printf '%(%c)T\r'; done &           # Some commands must be pushed into the background (`&`) to be successfully interrupted with keep open the terminal window.

exec bash                                          # Keep the window open after the above commands are finished or interrupted

3. Open Startup Applications and create a new entry. Its command should be:

gnome-terminal -x sh -c 'exec $HOME/run-and-minimize'

I also successfully have tested:

gnome-terminal -x bash -c 'exec $HOME/run-and-minimize'

References:

  • This command typed in a terminal window works: gnome-terminal -x bash -c "sudo ./serial"; xdotool getactivewindow windowminimize However when running the command in Startup Program the xdotool seems to be ignored. The pc boots with terminal windows on the screen. – Quickseal Oct 6 '17 at 15:07
  • Hi, @Quickseal, I'm asking just from curiosity. Have you tried this solution? It's interesting in which cases it shall not work. – pa4080 Oct 9 '17 at 17:31
  • Hello, @pa4080. I did try your steps but struggled in getting it working, so I decided to try the tmux option. It may be my fault getting all the syntax correct in the bash script, so for other's looking at this, it may work for them. – Quickseal Oct 10 '17 at 19:01
  • 1
    Worked like a charm. you might have to use xdotool getactivewindow windowminimize instead of xdotool windowminimize $(xdotool getactivewindow). askubuntu.com/a/912919/598062 – blueray Jul 10 '18 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.