Autostart with 2 programs

Hi,

I am using autostart script to run a program at at boot. Everything running fine but when I add a second program the second program not start.

Maybe doing something wrong or the scipt not supporting running more than one program at boot time?

This is how I configure it:

#!/bin/bash
# DietPi-Autostart custom script
# Location: /var/lib/dietpi/dietpi-autostart/custom.sh

sudo nice -n 19 Program 1

sudo nice -n 10 Program 2

exit 0

Some help would be grateful,

Regards

does the 2nd program depends on the first one? Maybe you would need to include a sleep command to delay start of 2nd program.

I tried with

sleep

it does not working at all via script even if i try to start one program. But when try on bash is working fine. And no the programs are not interfering each other.

you would need to add a time value to the sleep command like sleep 30 to have a 30 second delay.

Yes i have done with sleep 20 but not working when starting one program for test.

What happens if your run the commands from the script just in command line?
You get any errors?

You btw do not need to use sudo inside this script, it runs as root user anyway during boot. If the second program does not start, then the first does not stop. Indeed running those from console should help to investigate the issue, also you could run the whole script:

/var/lib/dietpi/dietpi-autostart/custom.sh

or

sudo /var/lib/dietpi/dietpi-autostart/custom.sh

Also you may check the logs:

journalctl -u dietpi-autostart_custom

another option would be to create a systemd service for each program you like to start.

Thank you for your answers.

When using sleep 20 command inside script and running as sudo I have this on logs:

 custom.sh[383]: sleep: invalid option -- '4'
 custom.sh[383]: Try 'sleep --help' for more information.

When I run without sleep 2 programs only one is starting. The second one is like not have put on the script.

Both programs must be stay open to be fully operational.

For systemd I have tried but it is a bit mess with runlevels, meaning that sometimes the services might start earlier or later and that create a huge mess.

Regards

Looks like a syntax error.
Can you post your script?
And which editor did you use? Maybe there are some invisible characers in it, which break the script.
You could try cat -v script.sh to show weird characters.

Yes the script is:

#!/bin/bash
# DietPi-Autostart custom script
# Location: /var/lib/dietpi/dietpi-autostart/custom.sh

sleep 20 nice -n 19 Program 1

exit 0

I modify with nano. I tried without nice with the same results. When running the command on bash without the script is working fine.

And with cat -v no weird characters are there.

Try

#!/bin/bash
# DietPi-Autostart custom script
# Location: /var/lib/dietpi/dietpi-autostart/custom.sh

sleep 20

nice -n 19 Program 1

exit 0

Yes with this way working thank you. With one program the sleep command working, it is a bit strange to be on different line from the main program command but it works.

Trying with second program to start but still no success. Both program must be staying running to work. I am thinking if there is a way to start automatic 2 scripts to boot is there a way to make this happen?

Regards.

Yes program 2 waits until program one is closed again, so you have to put a & at the end
https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script

#!/bin/bash
# DietPi-Autostart custom script
# Location: /var/lib/dietpi/dietpi-autostart/custom.sh

sudo nice -n 19 Program 1 &
sleep 5
sudo nice -n 10 Program 2 &
exit 0

The & puts the process in the background and continues with the next line

I had a further look and you maybe run into new issues:
https://www.maketecheasier.com/run-bash-commands-background-linux/
What definitely should work is to start them via cron at reboot.

Ok got it. With & solve the issue of 2 programs. So far is working but as you said if I see issues I will go to cron, this I haven’t thought it.

Thank you for your help.

Regards.

My concern is the ouput to the terminal, but I don’t know how this behaves when the process is started inside a script.
The last link also provides a solution for this:
Let the output post to /dev/null instead to STDOUT or STDERR (standard output and standard error output, which should be terminal, if you run headless?!)

the solution would be:
sudo nice -n 19 Program 1 &>/dev/null &

Still I recommend to create systemd services. Way easier to start each program individually.

What I am seeing inside the script when running both programs are working normal i do not print information on terminal or anything like this. I am seeing also that the script closing successfully after starting the programs. The &>/dev/null & I think is for programs that print on terminal and to avoid the conflict on bash it can be useful.

As for systemd to be working with my apps I have to configure every service to have the correct order so to be ok. The start up script is helping me because it start on the last or almost on the last runlevel and avoid all this. Of course if something goes wrong I have to do with the “hard way”. For now the information was very helpful.

Thank you Joulinar Jappe MichaIng for your help.

Regards. :slight_smile:

Just as a side note, I’d remove the & from the second program call. Otherwise the parent script exits before any program finished, which can cause issues, basically orphaned child processes.