Page 1 of 1

Bash Script when reboot

Posted: Wed Aug 15, 2018 1:44 pm
by defining
Hi,

I have a Bash script I would like to run when I run my Raspberry.

Here is my bash script (it works) :

Code: Select all

#!/bin/sh
cd /home/pi/Desktop/BTT/django_btt
git pull origin master
screen -S django_site -d -m python3 /home/pi/Desktop/BTT/django_btt/manage.py runserver 0.0.0.0:8080
I move it into the folder /etc/init.d

and then run sudo update-rc.d script_to_launch defaults

but it doesn't work as in the tutorials....why?? :(

Re: Bash Script when reboot

Posted: Wed Aug 15, 2018 1:54 pm
by DougieLawson
If you're going to stick with the old sysvinit stuff you need to add a bunch of LSB tags. It should also have "start", "stop" options in the script (take a look at /etc/init.d/skeleton if it still exists).

But with Raspbian Jessie, Raspbian Stretch and other current DebIan derivatives you should use a systemd service file to get your program running.

sudo nano /etc/systemd/system/django_thing.service

Code: Select all

[Unit]
Description=Django Thing

[Service]
ExecStart=/usr/local/bin/my_script_to_run

[Install]
WantedBy=multi-user.target
Then enable that with sudo systemctl enable django_thing.service

Re: Bash Script when reboot

Posted: Wed Aug 15, 2018 2:05 pm
by jbudd
Maybe nit-picking, but your script is [probably] interpreted as a Dash script not a Bash script.
On my Pi /bin/sh is a link to /bin/dash.
The Hashbang line for a Bash script is #! /bin/bash

It's good practice if a shell script is run automatically to use full pathnames for commands - in your case that's git and screen

Re: Bash Script when reboot

Posted: Wed Aug 15, 2018 2:07 pm
by defining
Maybe I haven't done the thing right but it doesn't work neither...:(

Re: Bash Script when reboot

Posted: Wed Aug 15, 2018 2:14 pm
by defining
Could someone give me the EXACT command (even with the extension) from the beginning to the end. I will correct it when I push it into my terminal

Re: Bash Script when reboot

Posted: Wed Aug 15, 2018 2:20 pm
by n67
Well, first of all, ignore any/all advice about systemd.

systemd may be good for the experts (it was designed for and by experts), but is useless for us ordinary folk.

This question is the most frequently asked of FAQs. There should be (but isn't) a straightforward, RPI-approved, answer to it.

There are sensible ways to do it using any/either of:

1) .profile
2) crontab
3) /etc/rc.local

The easiest and most straightforward to setup is the last in the above list.

Mind you, I said the easiest, but not necessarily the best. For my own projects, I always use #1 in the above list.

Anyway, there are lots of examples of various people's opinions on the subject strewn around this forum. It should not be too hard to find them.

Re: Bash Script when reboot

Posted: Wed Aug 15, 2018 2:22 pm
by B.Goode
From: https://www.gnu.org/software/screen/man ... l#Overview
Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. [ ... ]

When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would.

Is it possible/useful to invoke screen during the startup process?

Re: Bash Script when reboot

Posted: Wed Aug 15, 2018 2:26 pm
by n67
Is it possible/useful to invoke screen during the startup process?
Yes. I (and others) do it all the time.

Re: Bash Script when reboot

Posted: Wed Aug 15, 2018 2:28 pm
by RaTTuS
if you use systemd then you can set a user , env and have it all working fine
see
viewtopic.php?f=29&t=7192&p=828947#p828947
or
viewtopic.php?f=29&t=7192&start=25#p898424
or from heater
viewtopic.php?p=921354#p921354

Re: Bash Script when reboot

Posted: Wed Aug 15, 2018 2:29 pm
by B.Goode
n67 wrote:
Wed Aug 15, 2018 2:26 pm
Is it possible/useful to invoke screen during the startup process?
Yes. I (and others) do it all the time.
OK. Thanks

Re: Bash Script when reboot

Posted: Wed Aug 15, 2018 3:29 pm
by defining
Sorry if I'm rude but if I ask some help it is because Google doesn't work and not because I'm lazy. I needed someone to make with me step by step to find why my raspberry worked differently.

Nonetheless, I have found an alternative with crontab and everything work. I will look for the init.d problem another day!

Thanks a lot for your responses! And sorry if I was rude!!