View Full Version : keypress simulation needed to complete process automation

05-16-2012, 01:06 PM
Hey y'all. I wrote a daemon script to launch a second script that is responsible for monitoring/controlling a UI program. The problem is that if i run the daemon script on the command line like so: sh /path/to/daemon_script.sh, the monitor script causes the UI to not work correctly. Now if I run the contents of the daemon script manually on the command line like so: screen watch -n 60 /path/to/monitor_script.sh it works properly and runs every 60 seconds like it is supposed to. So, I am going to make a c program to see if it will work that way. I plan on using execl calls or something similar. Now here is where the problem lies; In order for the process to become daemonized, I need to manually hit control + A and control + D. That is what the screen command is for. Is there a way to simulate these key presses with C? I'll even settle for python, but would prefer C since that is what my entire program is based off of.

Even a better way to do this would be cool. Suggestions, ideas?


05-16-2012, 02:21 PM
Read through the man page for the screen utility. There are options that do exactly what you want. I'd tell you what they are, but that would take all the fun out of it :p.

05-16-2012, 05:46 PM
This blows! I am trying to find the easiest approach to this and it seems any direction I decide to take will be complex!

Someone else suggested to write a VTEmulator. Why write a few hundred line program for 150 lines of c code? If I were to write a VTE, I would definitely not do it in C. I would most likely do it in Python. I would say C++ but I dont like the feel of it and it is really ugly looking! No offense to any hard core C++ enthusiasts.

I was also told I could use openpty, forkpty, etc. But there is very little documentation, if any, it assumes you know whats going on and does a horrible job explaining.

The screen source; I have no idea what I'm looking for lol I guess that's why its so fun. Huh, Anduril?

IDK... <sighs>

05-16-2012, 05:52 PM
Want a little cheese with your whine? :p

Why do you think this is complex? What is difficult about starting your screen session/command in "detached" mode. There are options that the man page says are "useful for system startup scripts". Sounds like exactly what you want. So what exactly is the problem?

05-16-2012, 06:13 PM
I would love some!

Idk, someone in another forum made it seem so. Like I was going to have to write 300 lines of code to make it all possible.

automation with simulated keypresses in C programming (http://www.linuxquestions.org/questions/showthread.php?p=4680331#post4680331)

05-16-2012, 06:49 PM
This did it. I guess I was whining over nothing. I feel so dumb now!!

CMD=$(screen watch -n 60 /home/annonymous/Documents/monitor.sh | xdotool key control+a | xdotool key control+D) && echo $CMD;

05-16-2012, 07:00 PM
Read the bloody screen man page! Seriously! You would have had this done 4 hours ago if you would have just read the documentation. You don't need that xdotool crap.

05-16-2012, 07:09 PM
That actually just automates the daemon. Dint fix the issue. I still have the same problem; If i run a script that runs a script the UI does not print the open ports. The file does not even get written to. So if i run the monitord.sh file in /etc/init.d dir that runs the launcher/monitor script for the ui in my documents folder, it does not print to the file. The file is completely blank.

I will be back when i have the solution. To the man pages.

UPDATE: I used screens -dmS switch to replicate the above.

Ok it works with

screen -dmS name watch -n 60 /home/annonymous/Documents/monitor.sh

Thanks Anduril. Sorry for the aggrevation!