Iexpress
Posts: 9
Joined: Tue Dec 05, 2017 6:53 am

How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 1:21 am

Hi all, I was wondering how to make a Bash script located on removable optical media run automatically each time the optical disc is inserted within Raspbian?

I was wanting to send raw data to /dev/fb0, to show a custom graphical menu and it's selections via a BASH script.

Does Raspbian Stretch allow Autorun.inf files to run automatically when a disc is inserted?

Thanks!

asandford
Posts: 1785
Joined: Mon Dec 31, 2012 12:54 pm
Location: Ealing

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 1:30 am

Autorun is a Windows thing and not relevant to linux.
External media tends to get mounted automatically, and there must be examples on the web doing what you want.

User avatar
scruss
Posts: 1398
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 3:08 am

Not merely is Autorun discouraged under Linux, it's actively prevented. Removable media is mounted with the noexec option, which prevents any scripts/binaries being run automatically.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.


Iexpress
Posts: 9
Joined: Tue Dec 05, 2017 6:53 am

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 3:54 am

Thanks for explaining.

Maybe there's a way to use Udev, to check for when media is inserted. Maybe it can run a script and take appropriated action if it finds a certain file on the media.

I would probably name the script on the removable media autorun.sh, which I would like to use to load a series of raw images to /dev/fb0 that change depending on user input, giving an effect of scrolling through a menu.

Thanks again

ghans
Posts: 7431
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 7:17 am

udev seems the way forward. I can't think of anyother mechaanism to trigger scripts on
hotplug events.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

User avatar
DougieLawson
Posts: 30430
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 9:59 am

ghans wrote:
Thu Dec 07, 2017 7:17 am
udev seems the way forward. I can't think of anyother mechaanism to trigger scripts on
hotplug events.

ghans
Both of these work:

Code: Select all

KERNEL=="sd*", SUBSYSTEMS=="scsi", DRIVERS=="sd", SYMLINK+="sticker",  RUN+="/usr/local/bin/sticker_drive $kernel'"

Code: Select all

SUBSYSTEMS=="usb", ATTRS{removable}=="removable", RUN+="/usr/local/bin/sticker_drive $kernel'"
The script that runs is

Code: Select all

#!/usr/bin/python
import sys
import logging
import logging.handlers

my_logger = logging.getLogger('sticker_drive')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
my_logger.addHandler(handler)

my_logger.debug('Sticker drive active')
my_logger.debug('Argv[0]:'+sys.argv[0])
try:
  my_logger.debug('Argv[1]:'+sys.argv[1])
except:
  pass

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

my_logger.debug('Wiggling pin18 started')
for i in range(20):
  GPIO.output(18, 1)
  sleep(0.1)
  GPIO.output(18, 0)
  sleep(0.1)

my_logger.debug('Wiggling pin18 done')
You need to run a command like systemctl to get a long running service started. Udev scripts have a time limit before Udev shoots them dead.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

ghans
Posts: 7431
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 10:44 am

DougieLawson wrote:Udev scripts have a time limit before Udev shoots them dead.
Isn't there a way around that ? Like using "at" to schedule your script some seconds into the future ? It seems like no matter how deep you nest your bash scripts udev kills the whole process hierarchy.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

sparkie777
Posts: 48
Joined: Tue Nov 27, 2012 4:37 am

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 12:28 pm

ghans wrote:
Thu Dec 07, 2017 10:44 am
It seems like no matter how deep you nest your bash scripts udev kills the whole process hierarchy
did you already try to run the program in a new session?

something like this:

Code: Select all

setsid sh -c 'sleep 11; sleep 22; sleep 33' 1>&- 2>&- &
Initially, the new session has no controlling terminal. The calling process will be the only process in the new process group and in the new session. So udev should not get too much chance to kill it :)
Last edited by sparkie777 on Thu Dec 07, 2017 12:37 pm, edited 2 times in total.

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

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 12:30 pm

ghans wrote:
Thu Dec 07, 2017 10:44 am
DougieLawson wrote:Udev scripts have a time limit before Udev shoots them dead.
Isn't there a way around that ? Like using "at" to schedule your script some seconds into the future ? It seems like no matter how deep you nest your bash scripts udev kills the whole process hierarchy.
How about having something monitoring a file/directory/pipe and when your udev script pokes some information into that it reacts and does the work, no matter how long it takes, possibly forking off another process to do the actual work.

User avatar
DougieLawson
Posts: 30430
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 12:39 pm

rpdom wrote:
Thu Dec 07, 2017 12:30 pm
ghans wrote:
Thu Dec 07, 2017 10:44 am
DougieLawson wrote:Udev scripts have a time limit before Udev shoots them dead.
Isn't there a way around that ? Like using "at" to schedule your script some seconds into the future ? It seems like no matter how deep you nest your bash scripts udev kills the whole process hierarchy.
How about having something monitoring a file/directory/pipe and when your udev script pokes some information into that it reacts and does the work, no matter how long it takes, possibly forking off another process to do the actual work.
That could be a MQTT publish.

Running an AT job from the udev makes sense.
Starting a systemctl task makes sense.

You just can't fork or spawn a new task directly from the udev script.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

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

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 12:58 pm

DougieLawson wrote:
Thu Dec 07, 2017 12:39 pm
You just can't fork or spawn a new task directly from the udev script.
I never suggested that you should.

User avatar
DougieLawson
Posts: 30430
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Thu Dec 07, 2017 1:42 pm

rpdom wrote:
Thu Dec 07, 2017 12:58 pm
DougieLawson wrote:
Thu Dec 07, 2017 12:39 pm
You just can't fork or spawn a new task directly from the udev script.
I never suggested that you should.
I know, you said "send a message to another process to action the udev event" (paraphrased).
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.


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

Re: How to Autorun a Bash .sh script located on optical media when optical media is inserted?

Sun Dec 10, 2017 12:27 pm

DougieLawson wrote:
Thu Dec 07, 2017 1:42 pm
rpdom wrote:
Thu Dec 07, 2017 12:58 pm
DougieLawson wrote:
Thu Dec 07, 2017 12:39 pm
You just can't fork or spawn a new task directly from the udev script.
I never suggested that you should.
I know, you said "send a message to another process to action the udev event" (paraphrased).
Exactly :roll:

Return to “General programming discussion”

Who is online

Users browsing this forum: No registered users and 2 guests