Start script bash on start system/session

I am programming a script in bash and I want it to start when the system boots. I have tried everything from editing the init.d (with the included runlevels) to the graphical manager (Applications to Start) but nothing works. Can you help me?

Does your bash script include the “sudo” command? Perhaps it might help if you pasted the contents of the script here. I’ll take a look.

Sure… This is


#!/bin/bash
# Mostramos un mensaje
notify-send "CLICKenred:" "Lanzando programa - 5segs" --expire-time=5000
sleep 6
notify-send "CLICKenred:" "Lanzando programa - 10segs" --expire-time=5000
sleep 6
notify-send "CLICKenred:" "Lanzando programa - 15segs" --expire-time=5000
sleep 6
notify-send "CLICKenred:" "Lanzando programa - 20segs" --expire-time=5000
sleep 6
notify-send "CLICKenred:" "Lanzando programa - 25segs" --expire-time=5000
sleep 6
notify-send "CLICKenred:" "Lanzando programa - 30segs" --expire-time=5000
sleep 6

Thanks…

Sorry…

#!/bin/bash

LOL

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.