LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-09-2021, 09:31 AM   #1
shivanagouda.rm
LQ Newbie
 
Registered: Jun 2021
Posts: 4

Rep: Reputation: Disabled
Shell script sending some message to already running process(which written in c)


Can some one please help me on, communication mechanism between shell script and an daemon which is written C.

Want to exchange messages between shell script(bash) and an process.
 
Old 09-09-2021, 09:35 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 24,129

Rep: Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110
Quote:
Originally Posted by shivanagouda.rm View Post
Can some one please help me on, communication mechanism between shell script and an daemon which is written C. Want to exchange messages between shell script(bash) and an process.
Ok...so what, exactly, do you need help WITH?? You've posted no code, no details about what you're trying to do, etc. We can't guess. Shells typically have return codes on commands that can be read in a variety of ways.
 
1 members found this post helpful.
Old 09-09-2021, 09:50 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,788
Blog Entries: 3

Rep: Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007
Yes, more information is needed. However, as just a wild guess you might look into signals. See "man 7 signal".
 
1 members found this post helpful.
Old 09-09-2021, 10:00 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,396
Blog Entries: 13

Rep: Reputation: 4488Reputation: 4488Reputation: 4488Reputation: 4488Reputation: 4488Reputation: 4488Reputation: 4488Reputation: 4488Reputation: 4488Reputation: 4488Reputation: 4488
Quote:
Originally Posted by shivanagouda.rm View Post
Can some one please help me on, communication mechanism between shell script and an daemon which is written C.

Want to exchange messages between shell script(bash) and an process.
Signals and pipes are some options. You should read Chapter 6.

If you wish, in my name on the left side, there's a link to my blog overall and in there are several ancient discussions with examples for things like a daemon communicating with child processes, and so forth, also the use of the select() call, pipes, signals. It's spread around across the Linux Application category, or whatever I named it, there's 3 or 4 from 8-10 years ago.

For shell to process, there are signals and also the return status. Say for instance a shell script is run from within a Linux C application, that application can monitor using wait or waitv for the termination signals from that child (the script) and it can also check exit status.

If the script runs continually, well when it gets spawned from the C code, (or vice versa, script spawning a C application), pipes can be created in advance and then used to communicate between each side. Note pipes are half duplex, so you'll need 2 of them. Examples shown in those blogs.

This is not some "one of + quick" thing, you'll need to study the topic and decide what works for you and develop plus test the results incrementally. All the code shown in those blogs was not written in an afternoon, more like a few weeks or longer.

As others have said, we're happy to help, but you'll need to study up on the topic and try some things on your own to be able to ask next level types of questions.
 
Old 09-09-2021, 10:01 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,854

Rep: Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692
or you might want to check sockets, but there are a lot of different ways and without details hard to suggest anything
 
Old 09-10-2021, 12:08 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,118

Rep: Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687
There’s also DBUS, which tends to be missed when people list IPC mechanisms.

My questions are :

Does the daemon already accept messages in a certain format?

Are you allowed to modify the daemon, the shell script, or both?

Are the daemon and the shell script running on the same machine?

Is the daemon an existing program that we can look at?

Can you give us an example of a message you would like to send and receive?

Last edited by dugan; 09-10-2021 at 02:25 PM.
 
Old 09-14-2021, 09:13 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,291
Blog Entries: 4

Rep: Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318
The classical way to do this is with "pipes" – implemented by the "|" character on the command-line.

Every Linux/Unix process automatically has three file-handles: STDIN, STDOUT, STDERR.

The "C" program can be written to simply expect to read commands from its STDIN file, and to write its outputs to one of the other two. Now, you can simply issue a console command which invokes several programs at the same time, "piping" their inputs and outputs from one to the next. If you set things up in this way, you don't even have to write a separate program: the shell will do it for you.

It was a great idea in the 1970's, and it's still a great idea today.
 
Old 09-14-2021, 12:06 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,118

Rep: Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687
Quote:
Originally Posted by shivanagouda.rm View Post
Can some one please help me on, communication mechanism between shell script and a daemon which is written C.
Quote:
Originally Posted by sundialsvcs View Post
The classical way to do this is with "pipes"
Uh, no it's not. The very definition of a daemon is that it doesn't have a terminal, so you can't use pipes.

Last edited by dugan; 09-14-2021 at 12:13 PM.
 
Old 09-14-2021, 01:44 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,205
Blog Entries: 1

Rep: Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565
Named pipes, to be precise. The problem is they are unidirectional: you can send a request to a daemon via named pipe, but get no answer.
 
Old 09-14-2021, 08:45 PM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,291
Blog Entries: 4

Rep: Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318
To clarify – command-line "|" piping simply uses STDIN and STDOUT. The shell parses the command line, creates the child processes, pipes the STDOUT of one to the STDIN of the next (redirection of STDERR takes a few more keystrokes on your part), then lets them go. The shell then sleeps until all of the children terminate.
 
1 members found this post helpful.
Old 09-15-2021, 12:59 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,895

Rep: Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613
No response from OP so far...

I would ask first if OP has access to the running binary's src or manual.
We are assuming he does and it is written to be communicated with this way (or can be changed to do so).

It's entirely possible it's a 3rd party binary and it's not built that way ...

/pessimistic
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Please help me in writing python code which i have already written in c shravan1234 Programming 5 02-16-2020 08:15 PM
Shell Script : Kill a running process when another process starts ashmew2 Linux - General 3 08-20-2008 03:47 AM
Has someone already written a tutorial script? kedens Linux - General 1 10-10-2006 08:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 PM.

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