Start script bash on start system/session

Okay. So, you don’t have the problem of a sudo command not being executed in a startup script. That’s good.

That being the case, have you tried executing these commands in a terminal to see if they are having a problem? Make sure to copy and paste them from your script rather than re-writing them. That way, if there are any errors in the commands in your script, they will be replicated in your terminal command.

1 Like

Hi @isaaccueli, I have a quick mention to add. Could be nothing or everything. Did you try explicit path to notify-send?

/usr/bin/notify-send

1 Like

Just call the script from /etc/rc.local ???
This script is executed at boot time (no X and specify full path of command).

Regards,
BT

Hi,
Yes, i had done it. I execute the script in the line command, but i havent any error…
Thanks

Hi, Bill_MI

No… Why?.. I dont understand that you tell me… Sorry…

Hi, Bernard_Tremblay

Yes, i tried call to script from rc.local, but i dont get anything… Not execute…

Thanks…

Okay. So to recap and clarify:

  • You have run the commands inside this script one at a time, manually, in a terminal and they all work

  • You have run the script by double clicking it after you have logged in and it works.

  • None of the commands in the script require the entering of the user’s password

The next question I will ask is do you have any other process or processes running that is/are required as a prerequisite for these commands to work. That is say, are they/it definitely running when you test this script’s commands in a terminal, or by running the script after login, but which may not be running at startup?

Assuming the last issue mentioned is not applicable, then the reason the script is not working may be due to something called a “race condition”. This is where several scripts/commands are being run by the system at startup and, if there are too many being required to run simultaneously, some of them may get dropped. If that is what is happening with your script, you can fix this by delaying its operation by the insertion of a “sleep” command. In any event, putting in a sleep command will do no harm. This is how to do it:

Open your script and insert the following line above all of the other commands but after the #!/bin/bash command:

sleep 10

Save and close and re-login to see if it has fixed the problem.

You should check the syslog for error messages about your script.

Can you execute the script as root without errors ?
Open a shell and type “sudo -i” to log on as root with your password and be sure the environment will be the root environment not yours. Then call the script :slight_smile:

Regards,
BT

Just a minute: notify-send will send a message to the desktop. But yours desktop isn’t there at boot since X is not started yet… So you’re sending a note in /dev/null …

This command would make sense when a user login (you put the script in .config/autostart/ directory).
Otherwise it make absolutely no sense.

May be you should just explain what you want to do with the script. Some may have good ideas.

Regards,
BT

4 Likes

@isaaccueli: Which one do you want? “When system boots” is not the same as when session starts.

If you want a script to run at boot, forget rc.local since support for it may not be enabled by default and most likely won’t be there in the future. rc.local belongs to the SysVinit era, we are using systemd now.

Make an entry for it in crontab with magic word @reboot and it will be run at boot.

Test script /home/youruser/bin/boot-test (make executable):

#!/bin/bash
logger "This was run at boot"

Crontab entry:
@reboot /home/youruser/bin/boot-test

Reboot and check if the script was run:
journalctl -b |grep 'run at'

As for session start, you would go the autostart route (as you already know). You can use command “logger” with output (as above) to check, if the script was actually run. If that part is ok, but you still don’t get a notification, try telling notify-send where to send by adding “DISPLAY=:0” before the command:

DISPLAY=:0 notify-send ...

4 Likes

As @Bernard_Tremblay already pointed out, notify-send (which is used for desktop notifications) won’t notify nor send if there is no desktop. So using notify-send makes sense only in autostart type scripts.

For scripts ran at boot (rc.local type of things) use logging to a file if you need to get a “notification” (or confirmation, rather) that something was indeed run.

1 Like

Before all, my apologize for reply later… Sorry…
I am going to test yours soutions and i tell you… OK…???

Thanks, a lot…

Regards,

Isaac

Hi, again…

I had tried it tell me but not get.

OK… I am going explain better… My apologize…

I have a scrit with name “Pruebas.sh” in path “home/usuario/Descargas/Pruebas.sh”. This script contain this code:

#!/bin/bash
sleep 10
notify-send "Se ha iniciado el sistema" --expire-time=14000
sleep 15
exit

This code run OK in shell with user “Usuario” and user “root”. This script display a messagebox in desktop of Ubuntu Mate.

I would like run this script when system boot for i can see the messagebox “Se ha iniciado el sistema” around 14 seconds.

How can i to do it?

Thanks
regards,

Isaac

So are you trying to get the notification “bubble” to show during the boot sequence, before the login screen is shown?

You cannot use notify-send for that. It is a desktop notification application so it needs a desktop environment (=MATE) in order to work. No desktop = No notify-send.

If you need to display something during the boot, consider making a custom Plymouth theme.
https://wiki.ubuntu.com/Plymouth

🡱 Please ignore that. While maybe a good idea in theory, the way Plymouth currently works (or doesn’t work) this won’t help much.

These tests work, because the Ubuntu Mate desktop is running.

You can also send the message when X is starting.
See : https://debian-administration.org/article/50/Running_applications_automatically_when_X_starts

You have to put your script in the following folder : /etc/X11/Xsession.d

But the message will be send only when X is starting, not exactly when you boot the machine.

Regards,
BT

I’m guessing the need for this could be to cover the time of “black screen” before X comes up so that the user doesn’t press power button when thinking that nothing is happening. In that scenario the notification wouldn’t really serve a purpose after X is already up - it needs to be shown during the boot sequence.

Ok, I had to try this out myself (after reading the Spanish thread on this subject). Works as intended, I'd say:

What I did:

  1. Copy-pasted the script from your code above to /home/myusername/Lataukset/Pruebas.sh (Lataukset = Descargas in Finnish, the localized Downloads folder).
    #!/bin/bash sleep 10 notify-send "Se ha iniciado el sistema" --expire-time=14000 sleep 15 exit

  2. I made the script executable by the user (chmod 744 ~/Lataukset/Pruebas.sh)

  3. Added the script to Startup applications:

  1. Rebooted.

The notification appeared after 10 seconds of sleep, as intended - 5 seconds before my Conky came up (it's set to wait for 15 seconds).

1 Like

If i can find the place where i saw instructions about how to post some code, i’ll offer a possible solution, if i can find this thread again. Note to self: post contents of file “/etc/init/local_boot_init.conf”

/etc/init directory is for scripts used by Upstart, init system no longer in active use in Ubuntu.

Upstart was Canonical’s own init system, created to replace the venerable sysvinit (SysV) that served us well since 1983. Upstart was introduced in 6.10 (Edgy Eft) and became the default init system in 9.10 (Karmic Koala). In 2014 Debian announced they will follow all other main distros and adopt systemd, so Canonical decided to migrate from Upstart to systemd as well. Systemd became the default init system for Ubuntu in 15.04 (Vivid Vervet).

Upstart could run unmodified sysvinit init scripts in /etc/rc* directories making migration easier. It also executed rc.local (another sysvinit thing) where you could dump commands for things you want to run at boot time.

Upstart’s /etc/init and sysvinit’s runlevel directories /etc/rc* still remain, but they will be removed eventually. It’s just a matter of time.

Therefore I suggest again: use systemd unit files for things you need to start at boot time and autostart for things that need to run at (desktop) session start.

Those are current technologies, used by other main distros and desktops as well. Upstart was pretty much a Canonical (Ubuntu) only thing.

Further reading:
https://en.wikipedia.org/wiki/Init#SYSV
https://en.wikipedia.org/wiki/Upstart#Adoption

2 Likes

Oh, sorry, i just remember using that config file with ubuntu 11.10 and thought it might still work.