I want to send a signal to a process including data (int) from a bash script. I know how to do that from a C program but I didn't find the way if exists from a script.
Note : Pipe isn't an option
Related
I'm trying to write a message on the Signal app only using command lines.
What I think I need to do is : echo "Hello World" > &fd_of_Signal
In Linux there is a file like : '/proc/pid_of_app/fd/0' but I didn't find it on MacOs.
So I need to find the FD with the lsof command.
I used the command lsof -c Signal to get every opened Signal files.
I end up having lots of Signal files opened, with a lot of different FD (in FD column), from 0r to 41.
When I use the lsof command for another app (like Spotify), I have the exact same numbers in FD column.
My questions are : How can I differentiate which FD number should I use ? If they are similar for both apps, how can I make my command line understand that I want to use the fd for Signal and not Spotify ? In Linux, there is a file '/proc/pid_of_app/fd/0', is there something similar on MacOs ?
Is there a way to use input redirection after the program has started?
For example I want to run a program, scrape some data from it, then use that data to push it + some static data (from a file) to std input:
1 ./Binary
2 Hello the open machine is: computer2
3 Which computer:command do you want to use:
4 <<< "computer2:RunWaterPlants"
I want to redirect line 4 in using some program output from line 2.
I've tried Keeping a bash script running along the program it has started, and sending the program input, but it will just continue with the program execution without waiting for my input.
I can't edit ./Binary.
I found Write to stdin of a running process using pipe and it works for what I'm asking, but I can't see the stdout when I run it with pipe.
I figured it out from Writing to stdin of a process. Pretty much I started a fifo pipe and wrote to it and let it listen for input.
Right now, I have the following code to capture a character in ruby using the IO.read,
tty_param = `stty -g`
system 'stty raw -echo'
capt = IO.read '/dev/stdin', 1
system "stty #{tty_param}"
The code is called through a key-binding, so I'll be in the middle of an application like vim or just the bash prompt, when this is called.
What I'm wondering is, how do I send the characters back to my process once I've finished running my program?
Open3 can be what you are looking for.
Open3 grants you access to stdin, stdout, stderr and a thread to wait
for the child process when running another program. You can specify
various attributes, redirections, current directory, etc., of the
program in the same way as for Process.spawn.
How to read control c, control z in shell script?
Thanks in advance
added...
What is my requirement was, im deleting a file at the end of script. If the script was stopped (by control c or control z) also i need to delete that file
Every time an user make control c (or any other special combination) a signal are send to your script.
You will need to capture this signal in your script using the trap command.
It's long to explain, but this web contain a good explanation about managing signals: http://linuxcommand.org/wss0160.php
#!/bin/sh
trap 'echo Hi there' INT USR1 TERM
while true; do sleep 1; done
Read man kill for the list of allowed signals that you can put there, note the description field in the SIGNALS section of the kill man page that mentions which signal can be blocked (trapped) by your shell script.
Note: Ctrl + c is the INT (interrupt) signal
I want to understand that how does shell executes piped commands ? e.g. cat | more. I am aware that for executing a normal command shell does a fork, execute it and then child returns. But how does shell internally handle the execution of piped commands ?
Considering for example cat | grep, the shell first forks itself to start cat, and then forks itself once more to start grep.
Before calling one of the exec* family of functions in the two newly created processes to start the two programs, the tricky part is setting up the pipe and redirecting the descriptors. The pipe(2) system call is used in the shell process before forking to return a pair of descriptors which both children inherit - a reading end and a writing end.
The reading end will be closed in the first process (cat), and stdout will be redirected to the writing end using the dup2(2) system call. Similarly, the writing end in the second process (grep) will be closed and stdin will be redirected to the reading end again using dup2(2).
This way both programs are unaware of a pipe because they just work with the standard input/output.
It sets up a pipe using the pipe system call, forks two processes instead of one and attaches one end of the pipe to the first process' stdout and the other end to the second process' stdin.
The same, just the stdout of one application is the same as the next stdin. http://unixwiz.net/techtips/remap-pipe-fds.html