11

I have setup some applications to startup on each login (e.g., redshift-gtk, gtg) automatically but after adding these to startup applications (System -> Preferences -> Startup Applications) obviously the time taken to login has increased. Due to all this the time it takes for my panels, desktop etc to appear is too long - until which I am forced to wait.

I don't need these apps to be available immediately, but it would be good if they startup eventually, meanwhile the ubuntu menu/panel is available for running other apps that I might need to.

I tried using at command, with the intention of editing all startup applications to put the commands in the at queue, but this didn't work since the apps don't get the necessary environment variables (like DISPLAY).

Is this what nice command is used for? Any other ideas how I can accomplish this? If possible, I would like to avoid editing the startup applications commands, since this would mean a lot of effort to replicate on other machines I use.

Amith KK
  • 13,412
koushik
  • 5,072

3 Answers3

11

The number of seconds needed to wait for your desktop to load is arbitrary and can change depending on the situation. Instead of sleep, try using the following to run startup applications as soon as the system load has declined:

(Edit: Added koushik's suggestion.)

#!/bin/bash
# 
# Delays running an application until the system load has declined.
# 
# Usage:
#   run-when-load-low 'your command here'

echo "export DISPLAY=$DISPLAY; $1 &" | batch

exit 0

Save it as ~/bin/run-when-load-low and add run-when-load-low 'COMMAND' in Startup Applications Preferences.

Notes on this method:

  • The script above is what has worked for me. It passes only the DISPLAY environment variable to the application. For most desktop applications this will be all you need. With that said, be sure to consider any special cases and keep this fact in mind when troubleshooting anything that isn't behaving correctly. A good place to start if you think an application might need other environment variables passed is printenv and the application's documentation, though I personally haven't run into this problem yet.
  • My understanding of the system "load" value is that it does take into account IO waits, so delayed applications should not accidentally start too soon during the CPU usage lulls caused by desktop processes waiting on IO. This is not an area I know much about though, so please correct me if I am wrong here.
  • batch only affects when applications are run; it does not alter their priority/niceness.
  • This should go without saying, but if your system always has a high load, applications scheduled using this method might never run.
  • If you need to run an application with a parameter that contains spaces, you can escape them using the backslash: run-when-load-low 'gedit My\ Notes.txt'. If you really need to pass single-quoted parameters to your application, you'll have to use double quotes in the startup command: run-when-load-low "gedit 'My Notes.txt'". For anything more complicated than this, you're probably best off just modifying a copy of the script with your command hard-coded.
ændrük
  • 76,794
  • aendruk, thanks. this sounds like the optimal approach. 2 doubts remain, is DISPLAY the only variable needed by applications from my current environment which is not passed on by batch ? Also, is it possible cpu load reduces before the desktop has fully finished loading (say, waiting for disk for some icons to load) and these apps get started resulting in similar situation as before ? In other words, are the commands run with batch inherently lower priority that yield when other apps want to run ? – koushik Sep 08 '10 at 04:43
  • @koushik Thanks for bringing up these points. I've updated my answer to address them. – ændrük Sep 08 '10 at 05:47
  • Awesome, I had been looking for a solution to this problem for a while. – levesque Sep 08 '10 at 13:30
  • aendruk - this works very well. One question still remains: I think the original command line of the startup application should be enclosed in single-quotes '' for this to work. If the command line of the startup application has some parameters already enclosed in single-quotes, then how can I quote the command-line so that everything gets included in the input to batch ? If I understand correctly, only $1 is passed on to batch so the whole command line with quotes and everything should appear to this script as it first parameter, correct ? – koushik Sep 13 '10 at 14:00
  • That's correct. And since you can't use single quotes inside single quotes, to do this you'd have to surround the application's command with double quotes instead. I've edited my answer to try to explain this, though I am a little fuzzy on the nuances of nested quoting in Bash, so please feel free to correct me if I've made a mistake somewhere. – ændrük Sep 15 '10 at 18:53
  • 1
    This is now really helping me reduce time to login. Just to keep abreast of these apps eventually starting up, I have modified your script slightly to inform me when these apps actually run. Also unless the process forks to background, it stays on the batch queue as still running, so I made the process startup in background as well. The modified line is as follows: echo "export DISPLAY=$DISPLAY; notify-send 'Running on Load low' '$1' && $1 &" | batch – koushik Sep 17 '10 at 10:22
  • ... that way I know when atq is empty all my startup apps have run. – koushik Sep 17 '10 at 10:22
  • Nice improvements. The one thing I might do differently is use ; instead of && since I think I'd want the application to run even when notify-send fails, especially since it isn't installed by default. I'll edit my answer to include the & since I think this is really important. – ændrük Sep 17 '10 at 13:49
  • 1
    @ændrük I always refer to Greycat's wiki when I'm unsure about quoting and other 'best practices'. – aperson Sep 17 '10 at 21:35
5

I found that just using sleep 10 && COMMAND as the command name didn't work. I had issues with both conky and xchat loading too early and getting corrupted somehow. I had to write a little script called ~/bin/startup and put this in it:

#!/bin/bash
sleep 10
xchat &
conky &

You need change its permissions to be allowed to be executed with chmod +x ~/bin/startup and then just replace the start-up applications entry with the command startup and it'll fire off everything in the script.

You could have one file for each application if you were that way inclined.

Oli
  • 293,335
  • Additionally you could prefix the file or folder with a period (e.g. ~/.bin/startup) so it is hidden, in other words you don't see it when you look in your folders in nautilus, etc (well ctrl+h to toggle showing) – 8128 Sep 07 '10 at 19:54
  • I have had trouble with such a solution, sometimes it will work, sometimes it won't work. Also, the sleep will definitely delay your desktop booting. – levesque Sep 08 '10 at 10:59
  • 1
    The sleep will not delay booting. Start-up applications are non-blocking meaning that more than one thing can be loaded up at once. sleeping allows the rest of gnome to load and then load up the less-essential things. – Oli Sep 08 '10 at 12:01
2

Use the 'sleep' command.

In System -> Preferences -> Startup Applications, edit the command for the programs you want to delay to:

sleep 10 && COMMAND

Replace 10 with the number of seconds you want it to wait and COMMAND with what was in the command box originally.

dv3500ea
  • 37,204
  • Please read the full question text: OP complains that starting the apps takes too long, and asks for a way to "detach" them from the login process ("I don't need these apps to be available immediately, but it would be good if they startup eventually"). Adding a "sleep" in front of it will actually make the startup time larger. – Riccardo Murri Sep 07 '10 at 20:03
  • I didn't realise the login process waited for each command to finish. Maybe just using COMMAND & (run in background) would work. – dv3500ea Sep 07 '10 at 22:33
  • 1
    Sorry, my wrong: the login process does not wait for each and every startup command to return. Alas, I cannot remove my downvote unless you edit the question text... – Riccardo Murri Sep 09 '10 at 10:44