LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 12-18-2009, 05:06 AM   #1
cj_cheema
Member
 
Registered: Mar 2006
Location: INDIA
Distribution: RedHat, SuSE, Debian
Posts: 156

Rep: Reputation: 15
Question 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
 
Old 12-18-2009, 05:22 AM   #2
ammorais
Member
 
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182

Rep: Reputation: 49
You can use the system() command.
http://www.cplusplus.com/reference/c...stdlib/system/
 
1 members found this post helpful.
Old 12-18-2009, 12:59 PM   #3
cj_cheema
Member
 
Registered: Mar 2006
Location: INDIA
Distribution: RedHat, SuSE, Debian
Posts: 156

Original Poster
Rep: Reputation: 15
Smile

Quote:
Originally Posted by ammorais View Post
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
 
Old 12-18-2009, 01:07 PM   #4
MBybee
Member
 
Registered: Jan 2009
Location: wherever I can make a living
Distribution: PC-BSD / FreeBSD / Debian / Ubuntu / Win7 / OpenVMS
Posts: 438

Rep: Reputation: 57
Quote:
Originally Posted by cj_cheema View Post
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...
 
Old 12-18-2009, 01:49 PM   #5
cj_cheema
Member
 
Registered: Mar 2006
Location: INDIA
Distribution: RedHat, SuSE, Debian
Posts: 156

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by MBybee View Post
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
 
Old 12-18-2009, 04:48 PM   #6
ammorais
Member
 
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182

Rep: Reputation: 49
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
 
1 members found this post helpful.
Old 12-18-2009, 05:42 PM   #7
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 128Reputation: 128
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
 
Old 12-19-2009, 01:38 AM   #8
cj_cheema
Member
 
Registered: Mar 2006
Location: INDIA
Distribution: RedHat, SuSE, Debian
Posts: 156

Original Poster
Rep: Reputation: 15
Thanks friends for valuable tips and suggestions.....
 
  


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
process interaction in shell script a.toraby Programming 10 11-09-2008 11:02 AM
shell script to make a program run for a certain time Valkyrie_of_valhalla Programming 4 08-01-2006 07:09 PM
make c-program that accepts shell commands and execute them boeroe Programming 4 02-18-2005 09:41 AM
shell script interaction sudhasmyle Linux - Software 1 12-08-2004 04:34 PM
Interaction in shell script sudhasmyle Programming 1 12-08-2004 08:27 AM


All times are GMT -5. The time now is 11:56 PM.

Main Menu
Advertisement
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