execute script after desktop loaded?

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.


Solution 1:

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 :)

Solution 2:

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.

Solution 3:

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