defining
Posts: 39
Joined: Tue Mar 08, 2016 9:44 pm

Bash Script when reboot

Wed Aug 15, 2018 1:44 pm

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?? :(

User avatar
DougieLawson
Posts: 37666
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Bash Script when reboot

Wed Aug 15, 2018 1:54 pm

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
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

jbudd
Posts: 1143
Joined: Mon Dec 16, 2013 10:23 am

Re: Bash Script when reboot

Wed Aug 15, 2018 2:05 pm

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

defining
Posts: 39
Joined: Tue Mar 08, 2016 9:44 pm

Re: Bash Script when reboot

Wed Aug 15, 2018 2:07 pm

Maybe I haven't done the thing right but it doesn't work neither...:(

defining
Posts: 39
Joined: Tue Mar 08, 2016 9:44 pm

Re: Bash Script when reboot

Wed Aug 15, 2018 2:14 pm

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

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Bash Script when reboot

Wed Aug 15, 2018 2:20 pm

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.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
B.Goode
Posts: 9594
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Bash Script when reboot

Wed Aug 15, 2018 2:22 pm

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?

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Bash Script when reboot

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.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
RaTTuS
Posts: 10539
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Bash Script when reboot

Wed Aug 15, 2018 2:28 pm

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
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
B.Goode
Posts: 9594
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Bash Script when reboot

Wed Aug 15, 2018 2:29 pm

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

defining
Posts: 39
Joined: Tue Mar 08, 2016 9:44 pm

Re: Bash Script when reboot

Wed Aug 15, 2018 3:29 pm

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!!

Return to “Troubleshooting”