LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to make Interaction of C program with linux shell (https://www.linuxquestions.org/questions/linux-software-2/how-to-make-interaction-of-c-program-with-linux-shell-776629/)

cj_cheema 12-18-2009 04:06 AM

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

ammorais 12-18-2009 04:22 AM

You can use the system() command.
http://www.cplusplus.com/reference/c...stdlib/system/

cj_cheema 12-18-2009 11:59 AM

Quote:

Originally Posted by ammorais (Post 3796474)

Hi
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

MBybee 12-18-2009 12:07 PM

Quote:

Originally Posted by cj_cheema (Post 3796461)
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

I know I keep saying this, but ... why C? This would seem to be better suited to a shell script, largely because it would be easier for a newbie to modify.

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...

cj_cheema 12-18-2009 12:49 PM

Quote:

Originally Posted by MBybee (Post 3796990)
I know I keep saying this, but ... why C? This would seem to be better suited to a shell script, largely because it would be easier for a newbie to modify.

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...

You are right shell script is most easiest troubleshooting or customization perspective. But I am doing this for my own learning purpose and if you know C and C++ language and also having good knowledge in linux you can do lot tweak in linux isn't it!!

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

ammorais 12-18-2009 03:48 PM

Quote:

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.
Hi.

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

Elv13 12-18-2009 04:42 PM

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

cj_cheema 12-19-2009 12:38 AM

Thanks friends for valuable tips and suggestions.....


All times are GMT -5. The time now is 01:49 AM.