-1

I am trying to run 2 files, namely a python script called PaperCapture.py, and a bash script PrinterAutomation.sh simultaneously, since my task requires them to start at the same time. The python script is used to click pictures using a camera, and the bash script is used to send an echo signal to a printer, in order to print something and feed forward the paper.

I wrote another bash script called ScriptStart.sh which can start both these scripts simultaneously, but when I press Ctrl + C, or even try to kill the processes, the PrinterAutomation.sh script keeps running in the background, even when the terminal has been closed.

The code of ScriptStart.sh is below:

#!/bin/bash

#=============================================================================
# INITIAL SCRIPT FOR STARTING CAMERA & PRINTER SCRIPTS
#=============================================================================

python3 PaperCapture.py &  PIDIOS=$!
sudo sh ./PrinterAutomation.sh &  PIDMIX=$!
wait $PIDIOS

PID=pidof ./PrinterAutomation.sh
echo $PID
kill -INT $PID

If anyone can help me to figure this out, it would be great. All I need is for both the scripts to start simultaneously, and for PrinterAutomation.sh to stop running when PaperCapture.py has stopped running.

=======================================

UPDATE:

Removing pidof and using sudo kill -INT "$PIDMIX" terminates the scripts in the terminal foreground, but PrinterAutomation.sh still keeps running in the background.

This was fixed by adding the line

trap "kill 0" EXIT

at the top

Community
  • 1
  • 1
  • You save `PIDMIX` but then ignore it; why? `pidof` is somewhat more likely to return the wrong PID. – tripleee Feb 18 '20 at 11:52
  • I tried to use it instead of PID, but it gave the same result. So I assumed that pidof might give a different result, because this code worked one time – Ankit Billa Feb 18 '20 at 11:53
  • Concerning your question, two notes: Firstly, Bash is nothing Linux-specific. Secondly, process management in Bash is independent of the code the process is running, so here's nothing Python-specific either. If you had extracted a [mcve], you would have found at least the latter. As a new user here, please take the [tour] and read [ask]. – Ulrich Eckhardt Feb 18 '20 at 12:07

1 Answers1

1

You don't have permission to kill a process which isn't yours. Try

sudo kill -INT "$PIDMIX"

and probably get rid of the pointless and somewhat error-prone extra call to pidof (which also has a syntax error anyway; you probably meant

PID=$(pidof ./PrinterAutomation.sh)

or even better, prefer lower case for your private variables.)

tripleee
  • 175,061
  • 34
  • 275
  • 318