LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-20-2018, 03:13 PM   #1
carpa
LQ Newbie
 
Registered: May 2018
Location: Sao Carlos, Brazil
Distribution: Debian Jessie, Elementary Loki... in the past: Gentoo, Slackware, Suse
Posts: 8

Rep: Reputation: Disabled
Question 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?
 
Old 05-20-2018, 04:07 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 05-20-2018, 06:20 PM   #3
carpa
LQ Newbie
 
Registered: May 2018
Location: Sao Carlos, Brazil
Distribution: Debian Jessie, Elementary Loki... in the past: Gentoo, Slackware, Suse
Posts: 8

Original Poster
Rep: Reputation: Disabled
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.

Last edited by carpa; 05-20-2018 at 07:56 PM. Reason: Fixing ps output - clearing questions
 
Old 05-20-2018, 06:52 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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.
 
Old 05-20-2018, 07:00 PM   #5
carpa
LQ Newbie
 
Registered: May 2018
Location: Sao Carlos, Brazil
Distribution: Debian Jessie, Elementary Loki... in the past: Gentoo, Slackware, Suse
Posts: 8

Original Poster
Rep: Reputation: Disabled
@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...

Last edited by carpa; 05-20-2018 at 08:04 PM.
 
Old 05-20-2018, 08:12 PM   #6
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Did you try
Code:
program &
See this: https://stackoverflow.com/questions/...-in-background

Last edited by AwesomeMachine; 05-20-2018 at 08:15 PM.
 
Old 05-20-2018, 08:15 PM   #7
carpa
LQ Newbie
 
Registered: May 2018
Location: Sao Carlos, Brazil
Distribution: Debian Jessie, Elementary Loki... in the past: Gentoo, Slackware, Suse
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
Did you try
Code:
program &
I tried before and didn't work. Everything I tried inside script didn't work.
 
Old 05-20-2018, 11:19 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,732

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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...
 
Old 05-21-2018, 12:08 AM   #9
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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.
 
Old 05-21-2018, 03:43 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
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...)
 
Old 05-21-2018, 10:14 AM   #11
carpa
LQ Newbie
 
Registered: May 2018
Location: Sao Carlos, Brazil
Distribution: Debian Jessie, Elementary Loki... in the past: Gentoo, Slackware, Suse
Posts: 8

Original Poster
Rep: Reputation: Disabled
Hi!

Quote:
Originally Posted by AwesomeMachine View Post
Did you try
Code:
program &
See this: https://stackoverflow.com/questions/...-in-background
Thanks for the link!

Quote:
Originally Posted by scasey View Post
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 View Post
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!
 
Old 05-22-2018, 09:28 AM   #12
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 05-29-2018, 08:38 PM   #13
carpa
LQ Newbie
 
Registered: May 2018
Location: Sao Carlos, Brazil
Distribution: Debian Jessie, Elementary Loki... in the past: Gentoo, Slackware, Suse
Posts: 8

Original Poster
Rep: Reputation: Disabled
Jlinkels, sorry for answer you today.

Quote:
Originally Posted by jlinkels View Post
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.
 
Old 05-30-2018, 02:28 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
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.
 
Old 05-30-2018, 08:12 AM   #15
carpa
LQ Newbie
 
Registered: May 2018
Location: Sao Carlos, Brazil
Distribution: Debian Jessie, Elementary Loki... in the past: Gentoo, Slackware, Suse
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to Specify Running Time of a Background Process in a Bash Script julianvb Linux - Software 3 01-05-2016 12:50 PM
scripting, perl or bash; run a background process, get pid 2ck Programming 6 04-02-2010 12:16 AM
bash script to accept input ONLY until background process completes andrewstr Linux - Software 2 03-17-2004 12:02 PM
How to run a bash command in the background from perl script professorfrink Programming 3 11-13-2003 03:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration