5

I want to execute bash script on startup that opens several terminals in different workspaces. Script works just fine if I call it from terminal, but it doesn't work if executed from crontab using @reboot:

#!/usr/bin/env bash

#1 make sure we have enough workspaces
gconftool-2 --set -t int /apps/metacity/general/num_workspaces 7

#2. Launch programs in these terminals
wmctrl -s 6
gnome-terminal --full-screen --execute bash -c "tmux attach; bash"
wmctrl -s 5
gnome-terminal --full-screen --execute bash -c "weechat-curses; bash"
wmctrl -s 4
gnome-terminal --full-screen --execute bash -c "export TERM=xterm-256color; mutt; bash"
wmctrl -s 3 
gnome-terminal --full-screen
wmctrl -s 2
gnome-terminal --full-screen
wmctrl -s 1
gnome-terminal --full-screen
wmctrl -s 0
google-chrome --start-maximized

I think it's because crontab job triggers before desktop environment is loaded...maybe...? How can I execute this script after desktop environment is loaded? thanks:)

Update 1:

i've started it from crontab initially like this:

@reboot $HOME/andreiscripts/startup.sh >> $HOME/andreiscripts/testlog.txt 2>&1

and was getting these errors:

Cannot open display.
Failed to parse arguments: Cannot open display: 
Cannot open display.
Failed to parse arguments: Cannot open display: 
Cannot open display.
.....

Update 2

I've tried to launch script from System > Preferences > Startup Applications

/home/andrei/andreiscripts/startup.sh >> /home/andrei/Desktop/out.txt 2>&1

but script only opened first gnome-terminal in workspace 6... and wouldn't continue executing the rest of the script until I close that gnome-terminal and so on....

Update 3 - success

Look for my answer below to see the script that worked in the end.

2
  • Can you also paste in your crontab entry?
    – belacqua
    Feb 10, 2011 at 6:43
  • Can you split off Update3 into a seperate answer? Feb 11, 2011 at 23:22

4 Answers 4

4

Try this:

sh -c /home/andrei/andreiscripts/startup.sh >> /home/andrei/Desktop/out.txt 2>&1

That should work. I've run into this same problem and running my script with sh fixed it.

Also, you may want to delay it for 5 or 10 seconds so everything else can load. To do this, go to ~/.config/autostart, find your new entry, open it with a text editor, and add the line:

X-GNOME-Autostart-Delay=5

The number is in seconds. The same thing can be achieved with the "sleep" cmd, but this is cleaner (it spawns no new processes).

I've also run into problems trying to run shell scrips at startup running multiple commands that spawn multiple instances of programs like yours. The only solution I could come up with was to add an "&" to the end of each command. If "sh -c" doesn't work try that.

There are other possibilities too, if you still haven't got it working maybe we can try something else :)

4
  • thanks. tried adding sh -c - once again - only 1-st gnome terminal opens. second one waits for that first one to be closed and so one for each terminal.... gonna try & right now..
    – Stann
    Feb 11, 2011 at 17:37
  • ok.. if I add & to the end of each command or just gnome-terminal comands - all terminals open up in single workspace:)
    – Stann
    Feb 11, 2011 at 17:46
  • ok - i finally got it to work using & and sleep function :) yeahh.
    – Stann
    Feb 11, 2011 at 20:56
  • Good! I wasn't sure which would work. The & just tells it to keep processing the next command, regardless of the state of the last cmd. Autostarting scripts seems to work differently than when running scripts normally...haven't quite nailed down why yet :)
    – Deadite81
    Feb 13, 2011 at 10:54
4

Try System > Preferences > Startup Applications. You should be able to add an entry for your bash script. These applications are started as part of the post-login desktop load process.

1
  • mmm. it starts as expected but doesn't open second gnome-terminal instance until first one is closed. Do you know how to launch multiple gnome-terminals at the same time?
    – Stann
    Feb 11, 2011 at 3:39
1

Create 6 .desktop launchers to launch those commands, and then just save the session.

0

This is the script that works:

#!/usr/bin/env bash

#1 make sure we have enough workspaces
wmctl -n 7

#2. Launch programs in these terminals
gnome-terminal --title=tmux --full-screen --execute bash -c "tmux attach; bash" &
sleep 0.3
wmctrl -r :ACTIVE: -N "tmux"
gnome-terminal --full-screen --execute bash -c "weechat-curses; bash" &
sleep 0.3
wmctrl -r :ACTIVE: -N "weechat"
gnome-terminal --full-screen --execute bash -c "export TERM=xterm-256color; mutt; bash" &
sleep 0.3
wmctrl -r :ACTIVE: -N "mutt"
gnome-terminal --full-screen &
sleep 0.3
wmctrl -r :ACTIVE: -N "bash1"
gnome-terminal --full-screen &
sleep 0.3
wmctrl -r :ACTIVE: -N "bash2"
gnome-terminal --full-screen --execute bash -c "export TERM=xterm-256color;vim;bash" &
sleep 0.3
wmctrl -r :ACTIVE: -N "vim"
google-chrome --start-maximized &

sleep 2

wmctrl -r tmux -t 6
wmctrl -r weechat -t 5
wmctrl -r mutt -t 4
wmctrl -r bash2 -t 3
wmctrl -r bash1 -t 2
wmctrl -r vim -t 1
wmctrl -r Chrome -t 0

I start it from System > Preferences > Startup Applications as suggested by ayan4m1 like this:

/home/andrei/scripts/startup.sh >> /home/andrei/scripts/out.txt 2>&1
1
  • Good! You figured out that startup is not the same as login.
    – user535733
    Jan 18, 2014 at 3:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .