5

I need to run an application automatically when Ubuntu starts. I've got this app running without opening the terminal. However, this application displays some information in a terminal window and, therefore, I need that the app runs strictly after opening gnome-terminal (automatically). To this end I've tried to create a script file in init.d as it is explained here. The content of the script is:

#!/bin/bash
cd /home/mediambient/programa 
/usr/bin/gnome-terminal -e ./driver &

This works if I execute the file, but does not seem to do anything when I reboot the computer. Can anyone tell me what I'm doing wrong?

Thanks in advance.

2 Answers 2

8

You cannot make a script that runs from init.d and displays a gnome-terminal window, because scripts in init.d run before there is any login session in which to display one. They run even before the graphical user interface is running.

Logging to a File Instead

If you need to run this script when the machine boots up, then instead of trying to make it display text automatically, you should make it write its output to a log file. To make it run both regular output and error output to a log file, make the script as follows:

#!/bin/bash
cd /home/mediambient/programa
./driver &>> logfile

Replace logfile with the name of the log file you want to use. You might have that file in the /home/mediambient/programa folder, or you might put it in the folder for system log files (/var/log). It's your choice. Please note that the &>> operator will append the output to the end of the log file. If you want it to overwrite the log file each time, you should use the &> operator instead.

Running Commands from an Init Script as a non-root User

Please also note that scripts in init.d will be run as root. If you don't want that, but instead want to run programa as user mediambient, you can write the script as follows:

#!/bin/bash
cd /home/mediambient/programa
sudo -u mediambient ./driver &>> logfile

This runs the program as mediambient but still records the log file as root. If you also want to record the log file as mediambient (which you should probably do if you're putting it inside mediambient's home folder), you can use:

#!/bin/bash
cd /home/mediambient/programa
sudo -u mediambient ./driver 2>&1 | sudo -u mediambient tee logfile > /dev/null

(If you're interested in exactly how that works, this explains 2>&1 and this explains tee.)

(By the way, please note that nothing in this script specifically requires bash, which is an excellent interactive shell but is a bit resource-intensive to use for init and background scripts. For quite some time the default shell for running such scripts in Ubuntu has been dash, which is much more lightweight. If you use #!/bin/sh instead of #!/bin/bash, that uses the OS's preferred shell for running POSIX-style shell scripts. For this script, it doesn't matter much, but it's a good habit to get into.)

Running the Script When A User Logs On Graphically, Instead of at Boot Time

Alternatively, if what you really need is for the script to run when the user mediambient logs on graphically (rather than when the computer starts up), then you can make it run driver in a gnome-terminal window. You cannot use init.d for this; instead, you have to use the facility provided by your desktop environment.

If you're running a GNOME-based Ubuntu (with Unity, Unity 2D, GNOME Classic/Fallback, or GNOME Shell as your desktop environment)--which I guess you are, since you want to display the output in a gnome-terminal--then you can do this by pressing Alt+F2 and running gnome-session-properties. This gives you the opportunity to edit your Startup Applications. Put the following in the Command: field:

gnome-terminal --working-directory=/home/mediambient/programa -e ./driver

Running the Script at Boot Time, Logging to a File, and Automatically Viewing the Log when a User Logs On Graphically

Or you can (sort of) get the best of both worlds by putting the script in init.d to run when Ubuntu first starts up, and then automatically spawning a gnome-terminal window showing the output as it progresses, once mediambient logs in graphically. To do that, add an entry to Startup Applications with this command:

gnome-terminal --working-directory=/home/mediambient/programa -x tail -n 512 -f logfile

(Or if logfile is an absolute rather than relative path to the log file, then you can leave out --working-directory=/home/mediambient/programa.)

-n 512 means it displays up to 512 lines of the log file from before it started displaying the log file. (It will then display each line as it is added to the log file.) This is usually a reasonable choice since a gnome-terminal window only remembers up to 512 lines by default anyway. If you run gnome-terminal with a modified profile that remembers more than 512 lines, then you can edit the tail command accordingly.

Limiting the Size of the Log File

If you're concerned about the growing size of the log file, the first thing to do is figure out if this is actually likely to be a problem. You might just be able to estimate. Suppose the log file grows by one line per second (which is much faster than most log files) and each line is 80 characters long. Then in a month, the file is a little of 200 MB in size. In a year it grows to be about 2.5 GB. A couple hundred megabytes is probably fine and multiple gigabytes probably is not, so the question is, will you be able to manually edit the log file to clear it out, at least every few months?

If it's closer to one line per minute, then the file grows at a rate less than 50 MB per year, in which case you probably never have to worry about clearing out the file.

There are two simple ways to clear out the file. You can edit it and remove part (or all) of it. Or you can delete it, and let it be created again. If you delete it, it will continue to take up space on disk until nothing is accessing it anymore, which means you may need to restart driver. You'd also have to restart tail, but you can fix this problem by using a modified tail command that doesn't keep the file open:

gnome-terminal --working-directory=/home/mediambient/programa -x tail -n 512 --follow=name logfile

(I have replaced -f with --follow=name.)

For more advanced, or automatic, management of growing log files, you can use the logrotate command. The logrotate command checks to see if log files have exceeded their maximum size or age, and "rotates" them, compressing them into numbered archives (which are themselves eventually deleted) and making a fresh log file. logrotate will even send you email about the logs, if you set it up to do that. The logrotate manual page explains how to write configuration files to tell logrotate which logs to operate on.

You could run logrotate automatically from mediambient's user crontab. (A crontab lists tasks that are performed on a periodic schedule by the cron daemon.) The crontab command installs and operates on user crontabs. Or you could add an entry to the system crontab, /etc/crontab (which would be a reasonable choice if you're running the script in init.d and having it run driver as root rather than as as mediambient). Please note that Ubuntu's system logs are rotated with logrotate, though in Ubuntu's current default configuration, it is not called directly from any crontab.

This overview of log rotation with logrotate and task scheduling with cron should help you get started, but feel free to ask more detailed questions on AskUbuntu about these topics or, if you prefer, to comment here or edit your question to provide more detail about what you want to do (then I may be able to give more detailed explanations for how to do it).

2
  • That was really useful. Only one more question. This application is running indefinitely. Does that mean that the logfile will be also growing indefinitely? If so, i'll try with one the other option, since the memory is limited.
    – user65610
    May 23, 2012 at 16:21
  • @koco I have edited my answer to address this matter in some detail. (The log file may grow indefinitely but this is usually not a problem and when it is there are ways to mitigate it.) May 23, 2012 at 17:13
0

You have this written:

/usr/bin/gnome-terminal -e ./driver &

The & parameter which is at the end of the line means that the program is going to be hidden. This is useful for running background apps like web servers of boot sounds, but in your case you shouldn't use it. The solution is as simple as removing it:

/usr/bin/gnome-terminal -e ./driver

And I saw that you're not using GNOME for autostart. Use this instructions instead of the Server ones (replace google-chrome with the command above!): Chrome autostart on ubuntu

8
  • I tried it but the terminal window still doesn't appear.
    – koco
    May 23, 2012 at 15:21
  • Updated with more solutions! :)
    – Xerz
    May 23, 2012 at 15:33
  • Having & at the end of a command to run a GUI program doesn't hide the GUI. The & operator is inconsequential to the problem at hand here. The problem here is that you cannot run gnome-terminal with init (i.e., from a script in init.d) because there is no graphical user session running--X11 is not even running when those scripts are executed. If all the OP needs is for driver to run when the user mediambient (or another user with access to the run the program) logs on, then using Startup Applications will work, but you have to specify the working directory. May 23, 2012 at 16:02
  • & sometimes hides GUI apps, such Tomboy -- in fact, i'm using it, it hides the main menu but not the opened notes, which is very useful :)
    – Xerz
    May 23, 2012 at 18:01
  • & does not hide gnome-terminal's GUI (I have just double-checked this to make sure). That it hides parts of Tomboy's interface is interesting, and strange, and probably a bug. I'm looking into this now. Would you be willing to join me in chat to discuss this? (I don't think all our comments here should be moved to chat, so I think we should manually enter chat.) May 23, 2012 at 18:28

You must log in to answer this question.

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