bash script to start/stop ffmpeg x11grab

A application is storing its status in a file on the server /temp/status.txt. There are 4 status possible: wait, ready, recording, finished. With a cronjob I would like to start a shell script that checks this file and take action as follows:

  • wait -> do nothing
  • ready -> execute ffmpeg -f x11grab -s cif -r 25 -i :0.0 /tmp/out.mpg. Now while ffmpeg captures, the status.txt file still has to be checked for a change to the status,
  • recording -> do nothing
  • finished -> send q command to ffmpeg process to quit capturing process and finish script.

I tried to solve it with expect and xdotool to realize that this absolutely the wrong way. My problem is that I acutally have no experience in bash and handling processes, child processes etc.


I don't quite understand if your problem is that you don't know how to start writing the script at all or if you simply have a problem with a single one of your points Could you clarify your question?

I'm going to assume that your problem is that you don't know how to stop ffmpeg while it is running, the usual dirty way to stop a process is pkill, as in pkill ffmpeg, this should work for most purposes.

to send q to a running ffmpeg process, you could run ffmpeg inside a detached screen session, then send a string to the session.

#create a session
screen -d -m -S mysession
#prepare an ffmpeg command
cmd="ffmpeg -i input.avi output.mp4; exit"
#send the command to the session
screen -S mysession -p 0 -X stuff "$cmd^M"
#send the q string
screen -S mysession -p 0 -X stuff "q"

got this tip from

i'm sure you don't need this anymore. i just stumbled on the same problem and wanted to share it.

note: to insert that ^M after $cmd you have to hit ctrl+v then enter. the exit after the ffmpeg command closes the session after ffmpeg is finished.

Get The ProcessID and use,


     Process p=Process.GetProcessByID(int);

Need Your Help

Scrolling RecycleView that is inside AppBarLayout disables scrollFlag when scrolling outer RecycleView

android android-recyclerview android-coordinatorlayout android-appbarlayout

While i haven't touched the inner RecycleView, scrolling the outer RecycleView moves the content as desired, but after i have scrolled the inner RecycleView and then tried to scroll the outer Recyc...

Importing CRM Solution carries over the processes but some need to be re written


Whenever I export a solution with all working process and business rules the imported solution shows this error in the process section. I have to then rewrite the entire process. This is rare thoug...