Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send command to a background process

Tags:

bash

process

I have a previously running process (process1.sh) that is running in the background with a PID of 1111 (or some other arbitrary number). How could I send something like command option1 option2 to that process with a PID of 1111?

I don't want to start a new process1.sh!

like image 515
Mohit Deshpande Avatar asked May 13 '11 21:05

Mohit Deshpande


People also ask

How would you run a command as a background process?

If you want to run additional commands while a previous command runs, you can run a command in the background. If you know you want to run a command in the background, type an ampersand (&) after the command as shown in the following example. The number that follows is the process id.

How do you foreground a background process in Linux?

Bring a Process to Foreground in LinuxTo send the command to the background, you used 'bg'. To bring the background process back, use the command 'fg'. Now if you simply use fg, it will bring the last process in the background job queue to the foreground.

What is the key combination used to send a process to background Linux?

1 Answer. Show activity on this post. If you're using BASH, just press CTRL - Z , which will suspend the process, and then use the bg command to send it to the background.


3 Answers

Named Pipes are your friend. See the article Linux Journal: Using Named Pipes (FIFOs) with Bash.

like image 176
haknick Avatar answered Oct 22 '22 13:10

haknick


Based on the answers:

  1. Writing to stdin of background process
  2. Accessing bash command line args $@ vs $*
  3. Why my named pipe input command line just hangs when it is called?
  4. Can I redirect output to a log file and background a process at the same time?

I wrote two shell scripts to communicate with my game server.


This first script is run when computer start up. It does start the server and configure it to read/receive my commands while it run in background:

start_czero_server.sh

#!/bin/sh

# Go to the game server application folder where the game application `hlds_run` is
cd /home/user/Half-Life

# Set up a pipe named `/tmp/srv-input`
rm /tmp/srv-input
mkfifo /tmp/srv-input

# To avoid your server to receive a EOF. At least one process must have
# the fifo opened in writing so your server does not receive a EOF.
cat > /tmp/srv-input &

# The PID of this command is saved in the /tmp/srv-input-cat-pid file
# for latter kill.
# 
# To send a EOF to your server, you need to kill the `cat > /tmp/srv-input` process
# which PID has been saved in the `/tmp/srv-input-cat-pid file`.
echo $! > /tmp/srv-input-cat-pid

# Start the server reading from the pipe named `/tmp/srv-input`
# And also output all its console to the file `/home/user/Half-Life/my_logs.txt`
#
# Replace the `./hlds_run -console -game czero +port 27015` by your application command
./hlds_run -console -game czero +port 27015 > my_logs.txt 2>&1 < /tmp/srv-input &

# Successful execution 
exit 0

This second script it just a wrapper which allow me easily to send commands to the my server:

send.sh

half_life_folder="/home/jack/Steam/steamapps/common/Half-Life"

half_life_pid_tail_file_name=hlds_logs_tail_pid.txt
half_life_pid_tail="$(cat $half_life_folder/$half_life_pid_tail_file_name)"

if ps -p $half_life_pid_tail > /dev/null
then
    echo "$half_life_pid_tail is running"
else   
    echo "Starting the tailing..."
    tail -2f $half_life_folder/my_logs.txt &
    echo $! > $half_life_folder/$half_life_pid_tail_file_name
fi

echo "$@" > /tmp/srv-input
sleep 1

exit 0

Now every time I want to send a command to my server I just do on the terminal:

./send.sh mp_timelimit 30

This script allows me to keep tailing the process on your current terminal, because every time I send a command, it checks whether there is a tail process running in background. If not, it just start one and every time the process sends outputs, I can see it on the terminal I used to send the command, just like for the applications you run appending the & operator.


You could always keep another open terminal open just to listen to my server server console. To do it just use the tail command with the -f flag to follow my server console output:

./tail -f /home/user/Half-Life/my_logs.txt
like image 41
user Avatar answered Oct 22 '22 12:10

user


If you don't want to be limited to signals, your program must support one of the Inter Process Communication methods. See the corresponding Wikipedia article.

A simple method is to make it listen for commands on a Unix domain socket.

like image 2
Andre Holzner Avatar answered Oct 22 '22 11:10

Andre Holzner