Open applications in different workspaces in Gnome - x11

Given my laziness, I tried to write a bash script that opens at once some daily apps in different desktops. This script should work in Gnome. I've written that so far:
#!/bin/bash
firefox &
thunderbird &
/usr/bin/netbeans --locale en &
amsn &
gnome-terminal &
sleep 2
wmctrl -r firefox -t 0 && wmctrl -r netbeans -t 1 && wmctrl -r gnome-terminal -t 2 && wmctrl -r amsn -t 6 && wmctrl -r thunderbird -t 7
... but it doesn't work. My apps open, but they won't be assigned to the desktops I specify :(.
I changed the value of sleep to 15., but only firefox & netbeans are assigned correctly; the rest opens in the workspace where I execute the script from.

Thanks to Akira comment, I finally succeeded at making it work (the script runs at startup like a charm) Here is the new code:
#!/bin/bash
wmctrl -n 8
firefox &
thunderbird &
/usr/bin/netbeans --locale en &
amsn &
gnome-terminal &
sleep 15
wmctrl -r firefox -t 0
wmctrl -r netbeans -t 1
wmctrl -r terminal -t 2
wmctrl -r amsn -t 6
wmctrl -r thunderbird -t 7
#focus on terminal
wmctrl -a terminal

checkout DevilsPie, it watches creation of windows and act accordingly.
Devil's Pie can be configured to detect windows as they are created, and match the window to a set of rules. If the window matches the rules, it can perform a series of actions on that window. For example, I can make all windows created by X-Chat appear on all workspaces, and the main Gkrellm1 window does not appear in the pager or task list.
Or you can use a window manager which is able to do the same in-house, eg. fluxbox.

In dconf-editor:
org->gnome->shell->extensions->auto-move-windows
here is what it should look like:
['firefox.desktop:1','pidgin.desktop:2']

This script will check if it's required to change the workspace, switches to it, starts the app, waits until the window is created and switches back to the origin namespace.
Because it uses wmctrl -l to check if a new window was created, it can handle fast as well as slow started applications, without the need to wait for for a static amount of seconds.
I called this script start-on-workspace.
Usage: start-on-workspace <Workspace> <command> [argument...
#!/bin/sh -e
# get the target ns
target=$(($1 - 1))
shift
# get the current ns
current=$(wmctrl -d | grep '*' | cut -d' ' -f1)
if [ $current != target ]; then
# switch to target ns
wmctrl -s $target
fi
# get a checksum of currently running windows
a=$(wmctrl -l | cut -d' ' -f1 | sha1sum | cut -d' ' -f1)
b=$a
# start the app
$# &
# wait until there is a change on the window list
while [ $a = "$b" ]; do
a=$(wmctrl -l | cut -d' ' -f1 | sha1sum | cut -d' ' -f1)
sleep 0.1
done
# switch back to the origin namespace if needed
if [ $current != target ]; then
wmctrl -s $current
fi

Related

How to create a Tmux and Xpanes session into one splited window

I'm running a bash script that creates an Xpanes and Tmux sessions based on list of hosts (SSH to each host in the list with some extra arguments).
The issue now is that every xpanes command is created in a different window instead of one window with split panes.
If I'm running the same command separately (not via the script) it is work as excepted.
What am i missing here? or how can i attached all those windows to one splitted window?
Here is my code:
#!/bin/bash
#validate if TMUX session is running
if ! [ -n "$TMUX" ]; then
echo "This must be run from a Tmux session, please create a Tmux session first"
exit 0
fi
set +x
for srv in `cat hostsList.txt`; do \
echo "[$srv]"
RAND=`echo $srv | md5sum | awk '{print $1}'`
cp ~/.ssh/id_rsa{,_$RAND}
cp ~/.ssh/id_rsa{.pub,_$RAND.pub}
xpanes -c "{}" "<my SSH command>" ;
rm ~/.ssh/id_rsa_$RAND ~/.ssh/id_rsa_${RAND}.pub &
done

Make reconnect Openwrt script to run in boot and keep checking for current status

I have this that is working pretty good in openwrt:
#!/bin/sh
#set -x #debug
while true; do
wget -O /tmp/extip 'http://ifconfig.me/ip' 2>/dev/null
EXT_IP=$(cat /tmp/extip)
INT_IP="$(ip addr show dev wwan0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)"
ROOTER=/usr/lib/rooter
[ -z $EXT_IP ] && continue
if [ $(echo "$INT_IP" | grep "$EXT_IP" -q; echo $?) -eq 1 ]; then
/sbin/ifup wan1
$ROOTER/pwrtoggle.sh 1
echo "NAT-DETECT: External ($EXT_IP) does not match Internal ($INT_IP); Reconnecting WAN1..." > /dev/kmsg
/etc/init.d/firewall restart
/etc/init.d/miniupnpd restart
fi
sleep 30
done
#set +x #debug
How do I make this script run in the boot (or after sometime) and let it running in background forever checking current IP status and doing its job? Also it looks like it's not good to have a init script with "sleep", because it's considered bad form. What would be an optimized form of this script to match my purpose?
Thanks in advance!
I installed GNU screen on my OpenWRT and in /etc/rc.local I have something like this:
# start screen in detached mode
screen -dm -S boot -c /dev/null bash /root/bin/monitor.sh
It will run in background. You can also attach to it with screen -r boot after login.
You can try if it works for you.

Is there a way for tux to make status bar show different contents in different clients when they are all attached?

I write a simple shell script to help tmux show all the session in the status bar, however, the tmux's status bar always show the same thing in the different clients when they are all attached.
My script is as follows.
#!/usr/bin/env bash
session=`tmux ls | cut -d " " -f 1 | tr -d ":" | tr "\\n" " "`
# attached_count=`tmux ls | grep -n \(attached\) | cut -d ":" -f 1`
active_session=`tmux display-message -p '#S'`
# session_start="#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]"
session_start="#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]"
# session_end="#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]"
session_end="#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]"
session_active_start="#[fg=cyan,bg=black,nobold,noitalics,nounderscore]#[fg=black,bg=cyan]"
session_active_end="#[fg=black,bg=cyan,nobold,noitalics,nounderscore]"
session_str=""
count=1
for i in $session
do
# if [[ $count -eq $attached_count ]]; then
if [[ $i == $active_session ]]; then
session_str="${session_str}${session_active_start}${i}${session_active_end}"
else
session_str="${session_str}${session_start}${i}${session_end}"
fi
count=$((count+1))
# break
# echo "${i}"
done
session_str=`echo ${session_str} | tr -d "\n"`
printf "${session_str}"
```bash
It will output a long string content all my session name and make the attached one in other colors
"#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]besiii#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]conf#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]default#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]lang#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]#[fg=cyan,bg=black,nobold,noitalics,nounderscore]#[fg=black,bg=cyan]vert#[fg=black,bg=cyan,nobold,noitalics,nounderscore]"
However, all my iterm windows which attach tmux's client will show the same status bar.
Is there a way to make tmux show different contents in different attached clients??
TL;DR: No.
Tmux sessions are designed to show exactly the same thing for all connected users. If a user is connecting on a small terminal, everyone's view will be squashed to only show the small terminal size. There is no way around that (unless you're viewing different windows in the same session), and there is no way to show different content to different users when viewing the same window.

After run firefox go to full screen but not kiosk mode

I would like to start Firefox automatically on startup, and then maximize the window. With the code below, I am currently able to start Firefox, however I cannot get past that.
#!/bin/bash
firefox &
while [ "$(wmctrl -l | grep \"Mozilla Firefox\" 2>&1 | wc -l)" -eq 0 ];
do
sleep 1;
done
wmctrl -r firefox -b toggle, fullscreen
The first portion starts running Firefox. After that there is a loop, which I'm writing to create and display the Firefox window. The last step is maximize Firefox to full screen.
I believe I have a problem with the while loop.
I suggest:
while [[ $(wmctrl -l | grep "Mozilla Firefox" 2>&1 | wc -l) -eq 0 ]]; do

GNU Screen: How can I create a screen in the background if it doesn't exist?

"screen -R -D -S test" will create a session named test if it doesn't exist, or connect to it if it does
"screen -d -m -S test" will create a new detached session named test, whether it exists or not, possibly leading to multiple sessions named test:
There are several suitable screens on:
9705.test (06/18/2012 06:42:58 PM) (Detached)
9639.test (06/18/2012 06:42:57 PM) (Detached)
How can I create a detached session named test, but only if one doesn't already exist?
I believe you're looking for the -d -R combination:
screen -d -R -S test
From man screen:
-d -R Reattach a session and if necessary detach or even create it
first
EDIT
If you just want to create a background screen only if it doesn't exist, a little shell function in your ~/.bashrc or ~/.zshrc will work:
function bgsc {
if screen -list | awk '{print $1}' | grep -q "$1$"; then
echo "screen $1 already exists" > &2
else
screen -d -m -S $1
fi
}
Then just call bgsc test.

Resources