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