LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-16-2012, 01:57 PM   #1
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Linux Mint, Gentoo
Posts: 270

Rep: Reputation: Disabled
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?

Last edited by amboxer21; 05-16-2012 at 02:10 PM.
 
Old 05-16-2012, 03:17 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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.

Last edited by Nominal Animal; 05-16-2012 at 03:20 PM.
 
Old 05-16-2012, 05:28 PM   #3
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Linux Mint, Gentoo
Posts: 270

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-16-2012, 07:50 PM   #4
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Linux Mint, Gentoo
Posts: 270

Original Poster
Rep: Reputation: Disabled
Can't delete post, so I erased it. It Was irrelevant to the direction I was taking this post.

Last edited by amboxer21; 05-16-2012 at 08:33 PM.
 
Old 05-16-2012, 08:10 PM   #5
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Linux Mint, Gentoo
Posts: 270

Original Poster
Rep: Reputation: Disabled
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.

Last edited by amboxer21; 05-16-2012 at 09:29 PM.
 
Old 05-16-2012, 10:13 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by amboxer21 View Post
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 View Post
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.
 
Old 05-16-2012, 10:29 PM   #7
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Linux Mint, Gentoo
Posts: 270

Original Poster
Rep: Reputation: Disabled
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.

Last edited by amboxer21; 05-16-2012 at 10:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simulated Annealing vs. Stochastic Hill Climbing Chronothread Programming 2 02-28-2011 05:42 PM
simulated keyboard inputs under Linux MrUmunhum Linux - Software 1 07-04-2008 02:24 PM
Simulated mouse clicks in linux? devilkin Programming 2 02-14-2005 07:59 AM
Booting Kernel with simulated root file-system priyankadeven Linux - Newbie 2 12-29-2004 06:47 PM
Mapping keypresses to buttonpresses dhow Linux - Software 1 10-26-2004 06:39 PM


All times are GMT -5. The time now is 10:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration