ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to 'shutdown -hP TIME' my system everyday. I have a C program which runs on this PC and I want this shutdown command to be a part of that C program. This C program is launched automatically as I have added it in System->Preferences->Personal->Sessions (Fedora 10). Note that the C code does not run in a terminal.
I have tried using the following without success in my C program:
'system("sudo shutdown -h TIME");'
AND
#define SUDO "/usr/bin/sudo"
int main(void)
{
char *arg0;//this should be actual command itself
char *arg1,*arg2,*arg3,*arg4;
arg0 = "sudo";
arg1 = "shutdown";
arg2 = "-h";
arg3 = "01:30";
arg4 = (char *)NULL; execl(SUDO,arg0,arg1,arg2,arg3,arg4);
sleep(60*60);
return 0;
}
I can see that shutdown process isn't launched at all (ps -e |grep shutdown) in above examples, although other parts of the program run perfectly fine (even the code just above and below the shutdown command).
I have also seen that when I run the 'shutdown -h TIME &' command from a terminal, and close the terminal (exit) then the shutdown is also killed. It is not living beyond or without a terminal.
Can someone help me shutdown my computer using a timer without a terminal. (Please note that I do not want to use a terminal as it interferes with certain other processes).
Regards
Vijay
Last edited by vijay_babu1981; 07-05-2009 at 03:15 PM.
I'm studying C and I ain't sure at all, but I think there shouldn't be a "return" in "int main(void)", if you use void you mustn't put a return there Please correct me if I'm wrong...
If I understand you correctly, you simply want to shutdown your machine at the same time each day without any manual intervention. If this is the case the easiest way to do this is to set up a "cron" job (man cron) as root which runs the shutdown command for you.
I'm studying C and I ain't sure at all, but I think there shouldn't be a "return" in "int main(void)", if you use void you mustn't put a return there Please correct me if I'm wrong...
OK, I'll correct you, 'cause you're wrong.
The return type of 'main' here is 'int'. Hence "return 0;"
'void' is the argument type of 'main'
The first occurrence indicates the program to be run.
The second occurrence is the first parameter to the program. It is customary to have the first parameter of the program be the name of the program being run. For example, if you run this shell script:
arfasmif's comments seems useful. i will try to setup a cron job.
jlinkels comment "why do i put sudo twice in execl".
execl man page says:
The const char *arg and subsequent ellipses in the execl(), execlp(),
and execle() functions can be thought of as arg0, arg1, ..., argn.
Together they describe a list of one or more pointers to null-termi-
nated strings that represent the argument list available to the exe-
cuted program. The first argument, by convention, should point to the
filename associated with the file being executed. The list of argu-
ments must be terminated by a NULL pointer, and, since these are vari-
adic functions, this pointer must be cast (char *) NULL."
Uncle theodore and jlinkels command does not work:
execl("/usr/bin/sudo", "/sbin/shutdown", "-h", "1:30", (char*)NULL);
I get the following if I use this command:
[vijay@localhost tmp1]$ usage: shutdown -h | -K | -k | -L | -l | -V | -v
usage: shutdown [-bEHPS] [-r role] [-p prompt] [-t type] [-u username|#uid]
[VAR=value] {-i | -s | <command>}
usage: shutdown -e [-S] [-p prompt] [-u username|#uid] file ...
[1]+ Exit 1 ./shutdowntest
BTW, My original command with 2 sudo's work fine when the program is launched from a terminal and the terminal is kept open.
I am hoping the cron job should work! Any other suggestions would be welcome.
I know that argv[0] is the name of the program, but you don't have to pass yourself, it is placed for you there by the kernel.
No, it's placed there by whoever does the exec*() call. If you run a program from the command line, it's bash that passes the program name as argv[0], because it's bash that calls exec*().
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Hm, after all it is according to the man page if you realize that the first parameter is the first parameter in the arg list, not the first parameter in the exec* call.
For this specific call it looks redundant to pass both the command to be executed and the name of the file to be executed, but I guess that for other members of the exec* family it makes more sense.
For this specific call it looks redundant to pass both the command to be executed and the name of the file to be executed, but I guess that for other members of the exec* family it makes more sense.
When it comes to the significance of the arguments passed to the program about to be run, all members of the exec* family have the same meaning, but in different forms. And although the first parameter "means" the name of the file to be run, it can occasionally differ, with useful effect.
For example, if you use any member of the exec* family to run a program which is actually a symbolic link to another program, that second program will be run, but the argument will remain unchanged, because it was specified by the caller of the exec* function.
An example of this, at least on Slackware, is the poweroff command, which is a symbolic link to the halt command. The halt command examines argv[0] to see whether it was first called indeed as halt, or as something else. It thus knows what it's expected to do.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.