You are not logged in.

#1 2013-04-03 11:25:31

rick1908
Member
Registered: 2013-02-19
Posts: 5

udev script to start gscan2pdf when scanner is turned on

I got to try and make this quick before I go to work.

I want to start gscan2pdf when I turn on my scansnap scanner.

In order to do that I made a udev rule named "/etc/udev/rules.d/80-scansnap.rules" (permissions = root:root 644). The contents of the rule file is:

ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="04c5", ATTR{idProduct}=="11a2", GOTO="scansnap"
GOTO="scansnap_end"
LABEL="scansnap"
MODE="0664", GROUP="scanner", RUN+="/home/rick/scripts/scan_scansnap.sh"
LABEL="scansnap_end"

The script I am running scan_scansnap.sh is here. (permissions = rick:users 755)

I am logged into Arch (KDE) with my account (rick), I turn on the scanner and the gscan2pdf does not start. However, running the script scan_scansnap.sh manually performs as expected.

If I perform:

sudo udevadm test /devices/pci0000:00/0000:00:1a.7/usb2/2-3/2-3.4/2-3.4.2 > udev_scan_test_results

..I receive the following output.

Can anyone help me debug my error here?

Thank you for your help,
Rick

Last edited by rick1908 (2013-04-03 11:30:22)

Offline

#2 2013-04-05 14:58:24

rick1908
Member
Registered: 2013-02-19
Posts: 5

Re: udev script to start gscan2pdf when scanner is turned on

I have made a little more progress with my question. I have updated the script that starts gscan2pdf to this.

In testing the script, I execute it as sudo to simulate what udev is doing. When I do that, the script works great and everything is as expected. However, when udev executes the script, it (gscan2pdf) starts fines but then exits after a second or two. I have this feeling that is it has something to do with either the 'exit 0' statement and/or the '&' at the end of:

su $X11User -c "`gscan2pdf --device='fujitsu:ScanSnap S1500:373683'`" &

Anyone got any ideas to help me finish this one off?

Thanks,
Rick

BTW: I am now using sprunge instead of pastebin.com. I finally read this https://wiki.archlinux.org/index.php/Pa … in_Clients

Last edited by rick1908 (2013-04-05 15:00:14)

Offline

#3 2013-04-05 15:19:24

progandy
Member
Registered: 2012-05-17
Posts: 2,151

Re: udev script to start gscan2pdf when scanner is turned on

I think you'll have to "disown" the forked process or udev will kill it.
To properly detach a program from udev you could also try the daemonize wrapper from the AUR.

Last edited by progandy (2013-04-05 15:26:24)

Offline

#4 2013-04-05 15:55:51

rick1908
Member
Registered: 2013-02-19
Posts: 5

Re: udev script to start gscan2pdf when scanner is turned on

Looking at "daemonize — A tool to run a command as a daemon" from AUR...the upstream URl says:

From: http://software.clapper.org/daemonize/
    Close all open file descriptors (especially standard input, standard output and standard error)
    Change its working directory to the root filesystem, to ensure that it doesn’t tie up another filesystem and prevent it from being unmounted
    Reset its umask value
    Run in the background (i.e., fork)
    Disassociate from its process group (usually a shell), to insulate itself from signals (such as HUP) sent to the process group
    Ignore all terminal I/O signals
    Disassociate from the control terminal (and take steps not to reacquire one)
    Handle any SIGCLD signals

Wouldn't the first three points be detrimental to the application that was started by the user using 'su'?

I havn't tried it yet, it just reads like it may not be the solution. Anyhow, I will give it a try and see what happens.

Thanks for your help.

-Rick

EDIT: I missed this little tidbit in the udev manpage:

From: udev man page
RUN
           Add a program to the list of programs to be executed for a specific device.

           If no absolute path is given, the program is expected to live in /usr/lib/udev, otherwise the absolute path must be specified. The
           program name and following arguments are separated by spaces. Single quotes can be used to specify arguments with spaces.

           This can only be used for very short-running foreground tasks. Running an event process for a long period of time may block all
           further events for this or a dependent device.

           Starting daemons or other long running processes is not appropriate for udev; the forked processes, detached or not, will be
           unconditionally killed after the event handling has finished.

I guess I am back to the drawing board. This is an incredibly difficult process for such a simple request.

The ultimate goal is to just start an application when a usb device is connected. grrrrrr

-Rick

Last edited by rick1908 (2013-04-05 16:43:34)

Offline

#5 2013-07-02 12:22:33

kokoko3k
Member
Registered: 2008-11-14
Posts: 1,491

Re: udev script to start gscan2pdf when scanner is turned on

Hi, old thread, have you worked out that issue? i'm having a similar one hmm

Offline

#6 2013-07-02 14:09:11

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,095
Website

Re: udev script to start gscan2pdf when scanner is turned on

If you want to start a long running program from a udev rule, then create a systemd unit for it and use ENV{SYSTEMD_WANTS}="$fooservice.service" in the udev rule.

There is no way to "detach" a program from udev, unless you can remove the process from the cgroup it's started in.

Offline

#7 2013-07-03 10:27:06

kokoko3k
Member
Registered: 2008-11-14
Posts: 1,491

Re: udev script to start gscan2pdf when scanner is turned on

Thanks for the answer, but seems too complicated to me.
I've seen another solution that involves using "at":
Something like:

echo /path/to/script | at now

This means the atd service needs to be running, but nothing stops me from starting and stopping on the fly.
Going to try now...

-EDIT-
It works smile

Last edited by kokoko3k (2013-07-03 10:50:52)

Offline

#8 2013-07-03 16:19:00

kokoko3k
Member
Registered: 2008-11-14
Posts: 1,491

Re: udev script to start gscan2pdf when scanner is turned on

Ok, atd needs to keep running, or every process it spawned would get killed, even if i change their parent to init.

There is no way to "detach" a program from udev, unless you can remove the process from the cgroup it's started in.

I tried to change cgroup of the subprocess as you suggested, and the process went from systemd-udevd.service to a newly created subgroup.
Not only this; i even changed his parent to init via disown bash function.

Still, it has been killed.
...#@!
Sorry, do you have an explaination falconindy ?

Last edited by kokoko3k (2013-07-03 16:19:19)

Offline

#9 2014-05-28 11:55:58

kokoko3k
Member
Registered: 2008-11-14
Posts: 1,491

Re: udev script to start gscan2pdf when scanner is turned on

Offline

Board footer

Powered by FluxBB