LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   automation with simulated keypresses in C programming (http://www.linuxquestions.org/questions/programming-9/automation-with-simulated-keypresses-in-c-programming-945359/)

amboxer21 05-16-2012 02:57 PM

automation with simulated keypresses in C programming
 
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?

Nominal Animal 05-16-2012 04:17 PM

It looks like the program requires a terminal. Instead of using screen, you can always write your own terminal emulator, and control the program that way -- the emulator is the screen and the keyboard, as far as the program is concerned.

I think the libvterm library should help you in developing the terminal emulator; you should be able to provide all the functionality screen provides, but under complete control of your own program. If you don't need that much control, you could very likely just use forkpty() and emulate a simple, fixed terminal instead, by just reading from and writing to the master side of the terminal. That should take a few hundred lines of code, because there are quite a few ANSI escape sequences to handle, but it should be quite straightforward code.

You can also take a look at the screen sources for how it is done.

amboxer21 05-16-2012 06:28 PM

I really don't want to write a few hundred line program just to control a 150 line C program ya know. If I were to take that route, I would use an OOP language like Python. Even Ruby maybe?! I'll have a look at the screen source. At least I now have a starting point and know my options. Thanks Nominal.

amboxer21 05-16-2012 08:50 PM

Can't delete post, so I erased it. It Was irrelevant to the direction I was taking this post.

amboxer21 05-16-2012 09:10 PM

This fixed it Nominal:
Code:

screen -dmS name watch -n 60 /home/annonymous/Documents/monitor.sh
No need for a TermEmu, forkpty, etc.

Thanks for the help Nominal.

Nominal Animal 05-16-2012 11:13 PM

Quote:

Originally Posted by amboxer21 (Post 4680408)
This fixed it Nominal:
Code:

screen -dmS name watch -n 60 /home/annonymous/Documents/monitor.sh
No need for a TermEmu, forkpty, etc.

Thanks for the help Nominal.

So, the answer you were looking for was completely different than what you asked, right?

Quote:

Originally Posted by amboxer21 (Post 4680197)
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.

The solution you arrived at is something you should have realized immediately after even a quick glance through man 1 screen. I feel like I wasted my time and effort with you.

amboxer21 05-16-2012 11:29 PM

Wow man thanks for the kind words! I posted the same thing in another forum and that is how I came up with the solution. Another member pushed me in the right direction. Don't get mad because you were way off. Not my fault bro. Maybe you misunderstood my question? Not sure but thanks anyway.


All times are GMT -5. The time now is 07:43 PM.