Visit The New, Modern Unix Linux Community


Command timed out implementation


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Command timed out implementation
# 1  
Command timed out implementation

I have a running service which runs in background.
It execute shell commands by function
Code:
system(cmd)

I need to report fail when command execution takes more than 60 seconds.
Parent doesn't need to wait for 60 seconds of time if the cmd execution completed already.
Code:
runCommand()
{
  pid_t pid;
  pid = fork();
  if( pid  == 0 )
  {
      system(cmd); exit(0);
  }
  else if ( pid > 0)
  {
    sleep(60);
    childTerminated = waitpid( pid, 0, WNOHANG);
    if( childTerminated == 0 )
    {
      kill(pid, SIGKILL);
      timeout = true;
    }
    else
      timeout = false;
  }
}


Last edited by techmonk; 04-01-2018 at 09:41 AM.. Reason: indentation
# 2  
I think you better put the 60 second supervision into the child code, i.e. replace the system() with code that installs such a signal / -handler and execve() so it directly receives the signal.
Then in the parent code, you just need to waitpid().
But, to be honest, I do not have much practical experience with such C code...
This User Gave Thanks to MadeInGermany For This Post:
# 3  
I am actually calling sleep with 1 second in a loop, instead of 60 seconds to check if the command has already completed its execution.
# 4  
What is cmd?

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #153
Difficulty: Easy
Novell sold their Unix business operations to Microsoft in 1995.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX time command implementation

I want to know about the time command flow of execution. I have a doubt in the time calculation for the command execution. Whether the real time is sum of (time taken to open the unix window + execute the command given infront of the "time" command + close the unix window) or Just the time... (1 Reply)
Discussion started by: sateesh Solapur
1 Replies

2. Linux

Shell implementation - Command not found

Hi, I am trying to execute a program with pipes to run a few basic commands by forking children. When I try to run commands in the child process without pipe, I am unable to run the command as execv fails. However for commands that are given with pipes execute successfully. for example:... (1 Reply)
Discussion started by: mmurali2
1 Replies

3. Solaris

RCP command timed out

I HAVE A PERL SCRIPT WHICH RCP files from one server to another. The script is not having any issues for years and it is running for more than 3 years . Last week it had failed with error "Command timed out " error. Please help me out (3 Replies)
Discussion started by: praviper
3 Replies

4. Solaris

I/O timed out

I have Ultra 45 Sun solaris box with Solaris 10 installed. My problem is when i boot the unix box, i got the message: What does this message meant? then it does not continue to boot successfully. Please help. Thanks in advance. (5 Replies)
Discussion started by: etcpasswd
5 Replies

5. UNIX for Advanced & Expert Users

implementation of copy command in parallel

hey i have to implement copy command in parallel in c language. i dont know how to create a new directory in destination. if anything u know related to this help me (1 Reply)
Discussion started by: rajsekhar28
1 Replies

6. UNIX for Advanced & Expert Users

Implementation of ls - i command

It is possible for me to obtain the Inode of the path name using ls -i <pathname> command Can anyone tell me how its implemented... (3 Replies)
Discussion started by: ganapathy.psgit
3 Replies

7. UNIX for Dummies Questions & Answers

Timed read command

Hi guys, I love unix....but i also hate it :) I want to write a script that will pause in the middle ask for user input, but if no input is given i want the script to continue anyway. Say ask a question and give 1 min to answer and if no answer at all the script continues. (2 Replies)
Discussion started by: Noob e
2 Replies

8. Programming

Timed wait?

Is there any way in which I can make my wait signal to wait for a specified time for child job to complete. And if that time is over, the program gets out of the wait signal to process other things (4 Replies)
Discussion started by: anjul_thegreat
4 Replies

9. UNIX for Dummies Questions & Answers

timed commands

Hello, How can I set up events to be executed at a certain time? And do I need some kind of privilege such as being in cron group? (2 Replies)
Discussion started by: rayne
2 Replies

10. UNIX for Advanced & Expert Users

Connection Timed out

I connect to a Sun Box through telnet but it timed out in couple of minutes. Advance thanks for any idea...help... (2 Replies)
Discussion started by: s_aamir
2 Replies

Featured Tech Videos