[raspberry-vi] Re: Auto-start shell/batch scripts on a pi?

  • From: Jacob Kruger <jacob@xxxxxxxxxxxxx>
  • To: raspberry-vi@xxxxxxxxxxxxx
  • Date: Thu, 17 Nov 2016 21:09:38 +0200

Thanks, Tim


Main thing would be that have already set this pi to bootup into command 
line, and, will sort out auto-login, but, just want to either auto-run 
the python code, or let guys just type in something like the number 1 
and hit enter, but, yes, if auto ran the code, it would need to run 
after other processes.


Stay well


Jacob Kruger
Blind Biker
Skype: BlindZA
"Resistance is futile, but, acceptance is versatile..."

On 2016-11-17 8:52 PM, Tim Chase wrote:

On November 17, 2016, Jacob Kruger wrote:
Wondering what would need to do to implement a form of automatic
script startup on the pi booting up?
Depends on where you want to intercept in the boot process.

If you want it to run once as your user before you log in, the
easiest way I know is to edit your crontrab with

   $ crontab -e

(which will pull up your crontab in your $VISUAL or $EDITOR) and you
can then add a line like

   @reboot /home/jkruger/my_script.sh

which will run it once at startup.  Note that this runs like any
other daemon on the system, so wouldn't have interactivity.  You'd
have to implement some sort of auto-login and then launch your
program upon login by calling it from your ~/.bash_profile or
~/.profile file which is executed once when you log in.  It's a
little hairier if you use a GUI and can end up with multiple login
shells but that doesn't sound like your use-case.  And if you log in
on multiple virtual terminals or SSH into your box multiple times, the
script would get run each time you logged in. Not a *wrong* behavior,
just depends on what exactly you want.

what is the equivalent of a .bat file on a pi/under linux? .sh?
Yes, .sh is the usual extension used for shell scripts (the
unix/linux analog to .bat files on Windows).  They start with a
"shebang" line, usually

   #!/bin/sh

or

  #!/bin/bash

(note there's no space between the exclamation point and the opening
slash, as Jeffrey's reply accidentally added).

But you don't have to use a shell.  You can use any command
interpreter, including awk, sed, perl, python, ruby or whatever.

As in, if want to be able to launch a set of actions, or run a bit
of python code from something like a single character command, etc.,
then is there a way to do something like this?
If you have some basic logic that you'd do on a command prompt
(including possibly some basic "if" branching or for/while looping),
you can throw it in a shell script.

Alternatively, if you already have python code you want to run, you
can just add the corresponding shebang line at the top:

   #!/usr/bin/env python

and "chmod u+x my_script.py" and you can run it directly (assuming
it's on your $PATH or you specify its location).

Finally, you also have shell aliases and functions at your disposal.
Aliases are quite limited and are best for simple things like default
parameters or giving short-hand notations for longer commands.
Functions give the power of full shell-scripting and are a bit
handier.  You'd define either in your ~/.bashrc (or possibly aliases
in your ~/.bash_aliases if your configuration lets you do that).

For example, since can already get pi to boot up into command line
by default, then would want to try implement something like a
batch/script file called just 1, which would then change directory
to a specific path, and, start recording, or something, and, the
other issue there would be that to halt recording with arecord
think you'd need to either work with interaction, or hit ctrl+C, so
if could in fact get it to launch some python code, think should be
able to catch keystrokes, and, then pass other commands/variants on
to the shell, in the background, or something.
Yes, if you had a full keyboard a control+C would do the trick.  My
first thought would be to have your "1" script/alias launch your
recording program in the background and record its PID (process ID)
in a file.  It then returns you to the command prompt.  You could
then have "2" be a script/alias that kills off the recording process
by sending it a SIGINT which is the same as if you'd hit control+C
with the recording-process in the foreground.

Just a couple thoughts.

-tim


===========================================================
The raspberry-vi mailing list
Archives: //www.freelists.org/archives/raspberry-vi
Administrative contact: <mike.ray@xxxxxxxxxxxxxx>
-----------------------------------------------------------
Raspberry Pi and the Raspberry Pi logo are trademarks of the Raspberry Pi 
Foundation.

This list is not affiliated to the Raspberry Pi Foundation and the views and 
attitudes expressed by the subscribers to this list do not reflect those of 
the Foundation.

Mike Ray, list creator, January 2013




=========================================================== 
The raspberry-vi mailing list 
Archives: //www.freelists.org/archives/raspberry-vi
Administrative contact: <mike.ray@xxxxxxxxxxxxxx>
-----------------------------------------------------------
Raspberry Pi and the Raspberry Pi logo are trademarks of the Raspberry Pi 
Foundation.

This list is not affiliated to the Raspberry Pi Foundation and the views and 
attitudes expressed by the subscribers to this list do not reflect those of the 
Foundation.

Mike Ray, list creator, January 2013

Other related posts: