User avatar
RDK
Posts: 304
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Creating and using a script at startup

Sun Nov 29, 2015 6:23 pm

What I want to do seems simple to me but the more I Google and Read the more confused I get. Yes, I'm pretty much a novice with Linux.

What I want to do is basically the following during the boot-up process:
  • 1. run the " screen bash" command to create a screen session for the next step
  • 2. run the "python /mnt/usbkey/pgms/mypgm.py" command to start a Python program in that session
  • 3. exit the screen session with a "ctl-a + d" command. This will leave the Python program running in a disconnected mode which I can connect to later via PUTTY with the "screen -r" command if I need to.
These are the steps, after I logon, that I do manually after each reboot.

Practically, what code/script do I need to create and where does it need to go? And anything else that I need to do to make it all work,

Thanks for any help, references or clarification.....RDK

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Creating and using a script at startup

Sun Nov 29, 2015 7:28 pm

For steps 1, 2 and 3, you could use this command (you don't need bash, screen will use a shell anyway)

Code: Select all

screen -d -m /usr/bin/python my_python_script
That will start a screen session, use python to run your script, then detach from the session.

I'll leave the autostart bit for now. There are enough examples out there of how to do that (although many are out of date following the release of Jessie).

gerdesj
Posts: 24
Joined: Sun Nov 29, 2015 1:52 pm

Re: Creating and using a script at startup

Sun Nov 29, 2015 7:55 pm

After login type ls -la (the a will show all files, including hidden ones). Look for a file called .bashrc - the leading dot makes it a hidden file, ie it wont list normally unless you tell ls otherwise.

.bashrc is one of the scripts which a BASH shell will run on login. See https://unix.stackexchange.com/question ... es-it-work for more details. Basically put your startup stuff in there and it will run on login for that user. So on my laptop, I will find my bashrc in /home/gerdesj/.bashrc

I've just had a quick look on a PI in the office and /home/pi/.bashrc exists and is well commented including links to the docs - hooray.

Cheers
Jon

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Creating and using a script at startup

Sun Nov 29, 2015 8:21 pm

gerdesj wrote:...
.bashrc is one of the scripts which a BASH shell will run on login.
...
Forgive the nitpick, but "login" and "startup" are not the same thing.
If your system is set for auto-login then it will start a shell shortly after boot, but you can start a bash session (ie. a terminal) any time (in fact I do that very frequently).

look from "cron" and "@reboot" for a better alternative.

gerdesj
Posts: 24
Joined: Sun Nov 29, 2015 1:52 pm

Re: Creating and using a script at startup

Sun Nov 29, 2015 9:14 pm

sprinkmeier wrote:
gerdesj wrote:...
.bashrc is one of the scripts which a BASH shell will run on login.
...
Forgive the nitpick, but "login" and "startup" are not the same thing.
If your system is set for auto-login then it will start a shell shortly after boot, but you can start a bash session (ie. a terminal) any time (in fact I do that very frequently).

look from "cron" and "@reboot" for a better alternative.
Whoops, you are dead right and I didn't answer the right question!

/etc/rc.local is another place to put user boot up stuff. It has the advantage of being a method that has been used for years and is mentioned in many, many howtos from way before the Pi was even thought of.

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Creating and using a script at startup

Mon Nov 30, 2015 7:42 am

gerdesj wrote:/etc/rc.local is another place to put user boot up stuff. It has the advantage of being a method that has been used for years and is mentioned in many, many howtos from way before the Pi was even thought of.
and since the introduction of systemd is almost useless :(

User avatar
RDK
Posts: 304
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Creating and using a script at startup

Thu Dec 31, 2015 10:27 am

OK, I finally got back to this issue. I put the suggested script into the rc.local and it works.

BUT,.... the resulting SCREEN session is only available to the root user, which, in retrospect, makes sense as it is in charge during boot up and thus when the command is processed.

So, is there a way to specify which user or users can access the SCREEN session? Thanks....RDK

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

Re: Creating and using a script at startup

Thu Dec 31, 2015 10:37 am

RDK wrote: So, is there a way to specify which user or users can access the SCREEN session? Thanks....RDK
If you run it with the su command you can choose who owns the screen.

su pi -c 'screen ... parameters for screen ... etc.'
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

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

User avatar
RDK
Posts: 304
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Creating and using a script at startup

Thu Dec 31, 2015 1:42 pm

Cool. Where is that documented? Thanks....RDK

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Creating and using a script at startup

Thu Dec 31, 2015 2:26 pm

RDK wrote:Cool. Where is that documented? Thanks....RDK

Code: Select all

man su
and various web manuals. Googling for 'man <command>' actually works quite well

User avatar
RDK
Posts: 304
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Creating and using a script at startup

Thu Dec 31, 2015 2:56 pm

Blush

User avatar
RDK
Posts: 304
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Creating and using a script at startup

Sat Jan 02, 2016 6:11 pm

Hmmm, I have observed something different with Screen sessions created by the above boot-up script versus one I create manually. When I join the "boot-up" session with a "screen -r" all is fine and I see the Python pgm running. I have a "try" loop running in the Python pgm which looks for a ctl+c sequence when cause it to terminate normally, ie close all files, etc.

However, with the session started by the above boot-up script, when I hit ctl+c, it terminates the program AND the screen session. Is this normal?

Thanks.....RDK

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Creating and using a script at startup

Sat Jan 02, 2016 6:46 pm

I presume when you create a screen session manually you are just typing "screen", then starting the script from inside that?

If so, the thing that's running inside the screen session is the shell. The script is then being run in that shell. When the script exist it will return to the shell - still in the screen session. When you exit the shell, screen will terminate.

With the "boot up" screen command as given, you are running the script directly under screen, without an interactive shell session. Therefore when the script exits there is nothing left running under screen and the session terminates.

User avatar
RDK
Posts: 304
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Creating and using a script at startup

Sat Jan 02, 2016 7:18 pm

OK, thanks for the explanation. If I want it to behave like when I start the session manually, what do I have to do?...RDK

QuietZone
Posts: 89
Joined: Sat Dec 05, 2015 7:13 pm

Re: Creating and using a script at startup

Sat Jan 02, 2016 9:27 pm

RDK wrote:OK, thanks for the explanation. If I want it to behave like when I start the session manually, what do I have to do?...RDK
Do "man screen" and read up on the "stuff" command.

This allows you to, from you .screenrc file, create several interactive shell windows and run a specific program in each window - all as if you'd done it all manually. Once you get used to it, it is quite neat.
"If you haven't got anything nice to say about anybody come sit next to me." — Alice Roosevelt Longworth

User avatar
RDK
Posts: 304
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Creating and using a script at startup

Sun Jan 03, 2016 7:12 pm

Hmmm, I read the "Stuff" section in the manual and also did a bit of googling. I think this is going to take a bit of studying. Thanks for the direction....RDK

Return to “Beginners”