I am trying to create simple program which will run shell commands from arguments, for example
./run date +"%r"
07:56:05 PM
but I cant figure how. I try this, but it not working. I am pretty confused and absolutely cant figure how exec works..
#include <unistd.h>
#include <stdio.h>
int main (int argc, char *argv[]){
execlp("bash","bash", "argv[1]", (char*)0);
return 0;
}
You probably meant (note the lack of quotes around argv[1]):
execlp("bash", "myprogram", argv[1], NULL);
Note that I assume here that myprogram is a shell script. In case it's a binary, you should remove the preceding "bash" parameter.
One good troubleshooting technique could be replacing bash with echo to confirm the command line.
Related
default setting
setting changed.
How can I set this in command line?
I'm going to write a c program using system() call.
Thank you.
You can find some documentation on archlinux: https://wiki.archlinux.org/index.php/Vino
From a command line, you would have to type:
gsettings set org.gnome.Vino vnc-password $(echo -n 'mypasswd'|base64)
So, the equivalent C source should be:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char command[256];
sprintf(command, "gsettings set org.gnome.Vino vnc-password $(echo -n '%s'|base64)", argv[1]);
system(command);
return 0;
}
Note that this code lakes at least:
test that the program is called with one parameter
use snprintf instead of sprintf (to prevent writing to much data in command)
I'm currently trying to change the process name of a process so I can read the more easily with htop, top, .... I want to LD_PRELOAD this code into another process so it gets renamed by an environemt variable.
I found a lot of stuff in the internet, but nothing works:
prctl(PR_SET_NAME, "Test");
This does not work because htop is not honoring the name.
Nginx setproctitle (Link) doesn't work as well, because it strips the parameters (which are needed by the process).
I tried everything I found and now I'm out of ideas.
Is this even possible in linux? And how?
Just run your program by shell script or your program through exec and pass desired name as argv[0]:
#/bin/bash
exec -a fancy_name a.out ...
or C/C++:
execl( "./a.out", "fancy_name", ... );
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define NEW_NAME "hello_world"
int main(int argc, char **argv) {
if(strcmp(argv[0], NEW_NAME)) {
argv[0] = NEW_NAME;
execv("/proc/self/exe", argv);
fputs("exec failed", stderr);
return 1;
}
while(1) // so it goes to the top
;
}
I have shell scripts and I need to run that continuous work in background.
For example:
#include <stdio.h>
int main(int argc, char **argv)
{
for (; ;) {
system("./dup -r /root/duptest/");
sleep(60);
}
return 0;
}
It's working and run every minute.
First question: How can I run this background(like & --> ./dup ... &) without put &.
Second question: How can I put shell codes in C source codes?
I found this, Do I need to put \n\ for all lines? It's so hard for edit.
#include <stdio.h>
#include <stdlib.h>
#define SHELLSCRIPT "\
#/bin/bash \n\
echo \"hello\" \n\
echo \"how are you\" \n\
echo \"today\" \n\
"
int main()
{
system(SHELLSCRIPT);
return 0;
}
Third question: How can I use shell parameter in C, like this:
./dup.exe -r /blablabla...
mean
system("./dup -r /blablabla");
I need to use $1 $2 parameter with compiled C program.
Question 1: Look for "how to make a process as deamon process in UNIX" Although daemon process is a overkill for your requirement, you can perform steps until the process is running according to your requirements
Question 3: You need to have command line arguments, check about that. Your main should look like main(int arg_count, char *args_vector[]){...} and in that you can access each command line argument as an array element
Q1: use fork() and don't wait on the child's PID.
Q2: C and C++ will concatenate adjacent string literals, like so:
static const char script[] =
"echo hello\n"
"echo how are you\n"
"echo today"
;
int main(int argc, char* argv[])
{
puts(script); // so you can see what it looks like
// system(script); // <-- uncomment this line to actually run it.
return 0;
}
Q3: use the argc and argv parameters to main() to build the command line you want to execute.
I was wondering if there is a way to access the name of the standard input file within a C program. I've looked around, and haven't found anything. For example
./a.out < file.txt
Is there a way within the C program to print out the name of the file (file.txt)?
If you are using Linux, you could accomplish what you want by reading /proc/[pid]/fd/0 or parsing the out of lsof -p [pid], where [pid] is the process ID of your process.
See proce(5) and lsof(8).
printf("%s", argv[1]);
Make sure you have your main set up to accept command line arguments
int main(int argc, char** argv)
I have a c program running. I want to make the program sleep for certain period say 5 sec. I want this sleep to be induced from a text file containing command "sleep(5)". I want to pass this file through redirection operator (<) while executing the program
say ./a.out < samplefile.txt
This samplefile.txt contains sleep(5) command in it. I tried the above scenario but the c program is reading it as stream of characters like s,l,e,e,p which is not our intention.
could some one please help me on this issue.
You may make your program read the commands from the text file, parse them and behave as the commands say.
Instead of parsing, you may use the environment variables.
In your program, call to getenv in any place requires configuration. When calling to the program, make sure the environment is set with the required variables, or use this notation:
VAR1=VALUE1 VAR2=VALUE2 ./a.out
I'm not very clear what you want to achieve, but this may be a prompt.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char buf[512];
scanf("%s", buf);
if (!strcmp(buf, "sleep(5)")) {
printf("sleep...\n");
sleep(5);
}
printf("over\n");
return 0;
}