How to make Interaction of C program with linux shell
Hi
I am thinking for creating a program which help me or any person who has no much knowledge about linux in Linux server OS tuning. Say I want to create a program in C which will use to stop unnecessary services of linux at startup with the help of chkconfig and redirect service stop status in a text file. I am able to do same activity in shell script but I want to do in C. Please help. Thanks RGD Charanjit |
You can use the system() command.
http://www.cplusplus.com/reference/c...stdlib/system/ |
Quote:
Thanks Its work. I will try modify the command in this link provided by you as per my requirement. I will let you know if i face any problem. But Anyway Thanks once again for help. Regards Charanjit |
Quote:
You can't expect a newbie to decide they want a service you're stopping, read the C source, make the change, and then recompile it. Also - when it comes to Linux Server tuning... one would hope a sysadm is comfortable with /etc, init, inet, etc. They're going to have to spend a LOT of time with that config... |
Quote:
Ok let come to my issue: Hi ammorais I am creating a program which disable linux service at startup and this program should ask the user what service do he want to disable with the help of chkconfig and after disabling redirect chkconfig --list <service name> to file so that user can verify whether service has been disable or not and also if service is not installed or exist in server it gives an error message that service is not exist. But I am in confusion how do I create program pls help. Thanks Charanjit |
Quote:
I will have to agree with MBybee. This is not a task for C. If you want to learn C there are better ways than try to use C for what is not meant to. C is not meant to deal with output from other programs in execution. The system() command only returns the exit code of a program. You can't redirect output from the executed command to the calling process(at least with system()). If there is any other comand that do this, I don't know(and I don't really care). You can also have a look at execl. It's a similar function. One way doing this is to redirect the output of the executed command to a text file, and have the C program to load the file, parse the output, and go from there. But this is not the right way. The right way is like MBybee to program in shell. Regards |
Its not a job for C, even if a wrote a similar code in the past (but using ssl socket call to my cellphone java frontend).
If you want to get the result of the command (and not only the return code), you can use popen or do a fork + execvp |
Thanks friends for valuable tips and suggestions.....
|
All times are GMT -5. The time now is 01:49 AM. |