Xdotool does not minimize terminal window when using in Startup Application when pc boots?

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.


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:

  • Start detached Tmux | Tmux login script | Create session if | Command in detached Tmux

  • ArchWiki Tmux | A Guide to Customizing your Tmux.conf | A Gentle Introduction to Tmux

  • TMUX config file | Another example | Tmux Shortcuts & Cheatsheet, also here & here

Further reading:

  • Keep running a python program even after logging-off the ssh session

  • How to keep processes running after ending ssh session?


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:

  • What is the command to minimize the gnome terminal?