Rahul Bohare
Rahul Bohare

Reputation: 821

How to run two shell scripts at startup?

I am working with Ubuntu 16.04 and I have two shell scripts:

  1. run_roscore.sh : This one fires up a roscore in one terminal.
  2. run_detection_node.sh : This one starts an object detection node in another terminal and should start up once run_roscore.sh has initialized the roscore.

I need both the scripts to execute as soon as the system boots up.

I made both scripts executable and then added the following command to cron: @reboot /path/to/run_roscore.sh; /path/to/run_detection_node.sh, but it is not running.

I have also tried adding both scripts to the Startup Applications using this command for roscore: sh /path/to/run_roscore.sh and following command for detection node: sh /path/to/run_detection_node.sh. And it still does not work.

How do I get these scripts to run?

EDIT: I used the following command to see the system log for the CRON process: grep CRON /var/log/syslog and got the following output:

CRON[570]: (CRON) info (No MTA installed, discarding output).

So I installed MTA and then systemlog shows: CRON[597]: (nvidia) CMD (/path/to/run_roscore.sh; /path/to/run_detection_node.sh)

I am still not able to see the output (which is supposed to be a camera stream with detections, as I see it when I run the scripts directly in a terminal). How should I proceed?

Upvotes: 0

Views: 1564

Answers (3)

Rahul Bohare
Rahul Bohare

Reputation: 821

Since I got this working eventually, I am gonna answer my own question here.

I did the following steps to get the script running from startup:

  • Changed the type of the script from shell to bash (extension .bash).
  • Changed the shebang statement to be #!/bin/bash.
  • In Startup Applications, give the command bash path/to/script to run the script.

Basically when I changed the shell type from sh to bash, the script starts running as soon as the system boots up.

Note, in case this helps someone: My intention to have run_roscore.bash as a separate script was to run roscore as a background process. One can run it directly from a single script (which is also running the detection node) by having roscore& as a command before the rosnode starts. This command will fire up the master as a background process and leave the same terminal open for following commands to be executed.

Upvotes: 2

pitseeker
pitseeker

Reputation: 2543

If you want to make sure that "Roscore" (whatever it is) gets started when your Ubuntu starts up then you should start it as a service (not via cron). See this question/answer.

Upvotes: 0

nbari
nbari

Reputation: 26905

If you could install immortal you could use the require option to start in sequence your services, for example, this is could be the run config for /etc/immortal/script1.yml:

cmd: /path/to/script1
log:
    file: /var/log/script1.log
wait: 1
require:
  - script2

And for /etc/immortal/script2.yml

cmd: /path/to/script2
log:
    file: /var/log/script2.log

What this will do it will try to start both scripts on boot time, the first one script1 will wait 1 second before starting and also wait for script2 to be up and running, see more about the wait and require option here: https://immortal.run/post/immortal/

Based on your operating system you will need to configure/setup immortaldir, her is how to do it for Linux: https://immortal.run/post/how-to-install/

Going more deep in the topic of supervisors there are more alternatives here you could find some: https://en.wikipedia.org/wiki/Process_supervision

Upvotes: 0

Related Questions