LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script: Why process don't run in background? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-why-process-dont-run-in-background-4175630100/)

carpa 05-20-2018 03:13 PM

Bash script: Why process don't run in background?
 
Hello!

I'm trying to build a simple daemon and I'm struggling with bash. I'll try to explain what happens:

1. The execution works fine when I execute manually the command in the shell. Like:
# coproc software* param

2. When I try to execute the same line inside a bash script, the software is executed and ended immediately.

I tried use nohup, screen, coproc...

*software was written in C and waits for ENTER press command twice times to be ended.

Could anyone help?

jlinkels 05-20-2018 04:07 PM

Show exactly the commands that you issue.
Show the line(s) in the bash script executing your program.
Show terminal output.
Run your script with bash -x /path/to/script and copy relevant output here.

jlinkels

carpa 05-20-2018 06:20 PM

Hi, jlinkels!

Follows the script that executes the main software.

Code:

#!/bin/bash

# Killing all previously opened nfcDemoApp
killall -9 nfcDemoApp &> /dev/null

# Loading nfcDemoApp
#1. coproc nfcDemoApp poll
#2. /bin/bash -c "coproc nfcDemoApp poll"
#3. nohup /usr/local/sbin/nfcDemoApp poll&
#4. exec "coproc nfcDemoApp poll"
#5. $(/bin/bash -c "coproc /usr/local/sbin/nfcDemoApp poll")
#6. 
coproc nfc {
  /usr/local/sbin/nfcDemoApp poll
}

I've tried many ways to deal with this issue. :(

Code:

$ ./script
$  ps aux |grep nfc
user      27406  0.0  0.2  4272  2016 pts/1    Sl+  20:04  0:00 grep --color=auto nfc

Bash execution
Code:

$ bash -x script.sh
+ killall -9 nfcDemoApp
+ /usr/local/sbin/nfcDemoApp poll

Manually:
Code:

$ coproc nfcDemoApp poll
$ ps aux |grep nfc
user      27394  1.5  0.3  73188  3064 pts/1    Sl  20:04  0:00 nfcDemoApp poll
user      27406  0.0  0.2  4272  2016 pts/1    S+  20:04  0:00 grep --color=auto nfc


It seems that coproc is not invoked inside the script. I didn't expect this behavior.

AwesomeMachine 05-20-2018 06:52 PM

So you wrote coproc to prompt for user input? Or just to wait for user input? You didn't really respond to jlinkels requests. Maybe you could rewrite the script so it prompts for user input. I'm still not clear on what the problem is.

carpa 05-20-2018 07:00 PM

@AwesomeMachine,

I didn't understand. I think that I answered all questions. Did I make any mistake?

To make my question more clear:

The tool "nfcDemoApp poll" must be executed automatically after system boot. There isn't any type of user interaction.

The main problem is to put the tool running in the background. When I do manually it works but fails inside a script.

It's not common this issue in others applications... :(

AwesomeMachine 05-20-2018 08:12 PM

Did you try
Code:

program &
See this: https://stackoverflow.com/questions/...-in-background

carpa 05-20-2018 08:15 PM

Quote:

Originally Posted by AwesomeMachine (Post 5857227)
Did you try
Code:

program &

I tried before and didn't work. Everything I tried inside script didn't work. :(

scasey 05-20-2018 11:19 PM

What you're showing on the command line is not what's in the bash script...

...and nothing you've posted has anything to do with running in background...running in a script is not running in background.

So, I'm a tad confused about what your problem actually is...

To run the command in a script, put the command in the script. i.e.:
Code:

#!/bin/bash
coproc nfcDemoApp poll

I'm ignoring the bit about "background" here...

AwesomeMachine 05-21-2018 12:08 AM

He means the program terminates when it's called from the script, whereas it runs as a daemon if it's called from the command line.

MadeInGermany 05-21-2018 03:43 AM

Cut the default file handles and send to background
Code:

/usr/local/sbin/nfcDemoApp poll >/dev/null </dev/null 2>&1 &
Further you can trap the HUP signal or use nohup.
(Actually all that should happen in the daemon...)

carpa 05-21-2018 10:14 AM

Hi!

Quote:

Originally Posted by AwesomeMachine (Post 5857227)
Did you try
Code:

program &
See this: https://stackoverflow.com/questions/...-in-background

Thanks for the link!

Quote:

Originally Posted by scasey (Post 5857300)
What you're showing on the command line is not what's in the bash script...

...and nothing you've posted has anything to do with running in background...running in a script is not running in background.

So, I'm a tad confused about what your problem actually is...

To run the command in a script, put the command in the script. i.e.:
Code:

#!/bin/bash
coproc nfcDemoApp poll

I'm ignoring the bit about "background" here...

But I'm just trying to take it one step at a time to build the script. The most important thing is to run nfcDemoApp as a daemon.

I'm investigating the nfcDemoApp source code (developed by NXP) and this software apparently cannot be detached by the shell. The software takes input data from i2c (treating it like a keyboard or other input device) and when I tried to run the coproc command inside a script, I assume the software does not detect the input and closes because of that.


Quote:

Originally Posted by MadeInGermany (Post 5857365)
Cut the default file handles and send to background
Code:

/usr/local/sbin/nfcDemoApp poll >/dev/null </dev/null 2>&1 &
Further you can trap the HUP signal or use nohup.
(Actually all that should happen in the daemon...)

When I try to change input for </dev/null the software close immediately. I've tried before detaching in this way and fails.


Thanks for the attention, guys!

jlinkels 05-22-2018 09:28 AM

I might be totally on the wrong track, but when nfcDemoApp is called from a script, it is the child process of that script, isn't it? So the script runs, calls coproc. Coproc executes nfcDemoApp, backgrounds it and then returns to the calling script. The calling script terminates and kills all its child processes. Which is nfcDemoApp.

I.e. backgrounding does not disconnect the background program from the calling script. It is just backgrounded, which means stdin and stdout are disconnected from the keyboard and terminal.

If you start and background a program in the shell, and then close the shell. The backgrounded program will be closed as well.

Running from the shell, you can disown the backgrounded program. Then it continues to run. You can also start the program with nohup. Although I am not sure it continues when the script terminates. It continues when you close your shell.

I am totally not sure how all this works with coproc.

Summarizing, this should be working from withing a script.
Code:

/path/to/your/program >/dev/null  &disown
Use input and output redirection as required.

jlinkels

carpa 05-29-2018 08:38 PM

Jlinkels, sorry for answer you today.

Quote:

Originally Posted by jlinkels (Post 5857921)
I might be totally on the wrong track, but when nfcDemoApp is called from a script, it is the child process of that script, isn't it? So the script runs, calls coproc. Coproc executes nfcDemoApp, backgrounds it and then returns to the calling script. The calling script terminates and kills all its child processes. Which is nfcDemoApp.

Yes, you are right. Although, I tried everything: coproc, nohup as well disown. All these things didn't work.

Opening a new terminal after boot, doing automatic login and execute the software works. Yes, that is a poor solution, but was the unique alternative. :(

pan64 05-30-2018 02:28 AM

what I don't understand: you need to check the logs of that app, why it was died (if available).
If I understand well it runs from a terminal, but won't run without terminal. That is expected if user input was required, but no tty attached to the process.

carpa 05-30-2018 08:12 AM

Quote:

Originally Posted by pan64 (Post 5861202)
what I don't understand: you need to check the logs of that app, why it was died (if available).
If I understand well it runs from a terminal, but won't run without terminal. That is expected if user input was required, but no tty attached to the process.

pan64,
The app must be in continuous looping and never die.

The software runs without terminal but only if I type "coproc nfcDemoApp" in shell even I close the terminal after... It's weird.


All times are GMT -5. The time now is 02:50 AM.