Sponsored Content
Top Forums UNIX for Advanced & Expert Users Command timed out implementation Post 303015289 by techmonk on Sunday 1st of April 2018 08:40:33 AM
Old 04-01-2018
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
SyncExec(3pm)						User Contributed Perl Documentation					     SyncExec(3pm)

NAME
Proc::SyncExec - Spawn processes but report exec() errors SYNOPSIS
# Normal-looking piped opens which properly report exec() errors in $!: sync_open WRITER_FH, "|command -with args" or die $!; sync_open READER_FH, "command -with args|" or die $!; # Synchronized fork/exec which reports exec errors in $!: $pid = sync_exec $command, @arg; $pid = sync_exec $code_ref, $cmd, @arg; # run code after fork in kid # fork() which retries if it fails, then croaks() if it still fails. $pid = fork_retry; $pid = fork_retry 100; # retry 100 times rather than 5 $pid = fork_retry 100, 2; # sleep 2 rather than 5 seconds between # A couple of interfaces similar to sync_open() but which let you # avoid the shell: $pid = sync_fhpopen_noshell READERFH, 'r', @command; $pid = sync_fhpopen_noshell WRITERFH, 'w', @command; $fh = sync_popen_noshell 'r', @command_which_outputs; $fh = sync_popen_noshell 'w', @command_which_inputs; ($fh, $pid) = sync_popen_noshell 'r', @command_which_outputs; ($fh, $pid)= sync_popen_noshell 'w', @command_which_inputs; DESCRIPTION
This module contains functions for synchronized process spawning with full error return. If the child's exec() call fails the reason for the failure is reported back to the parent. These functions will croak() if they encounter an unexpected system error, such as a pipe() failure or a repeated fork() failure. Nothing is exported by default. fork_retry [max-retries [sleep-between]] This function runs fork() until it succeeds or until max-retries (default 5) attempts have been made, sleeping sleep-between seconds (default 5) between attempts. If the last fork() fails fork_retry croak()s. sync_exec [code] command... This function is similar to a fork()/exec() sequence but with a few twists. sync_exec does not return until after the fork()ed child has already performed its exec(). The synchronization this provides is useful in some unusual circumstances. Normally the pid of the child process is returned. However, if the child fails its exec() sync_exec returns undef and sets $! to the reason for the child's exec() failure. Since the @cmd array is passed directly to Perl's exec() Perl might choose to invoke the command via the shell if @cmd contains only one element and it looks like it needs a shell to interpret it. If this happens the return value of sync_exec only indicates whether the exec() of the shell worked. The optional initial code argument must be a code reference. If it is present it is run in the child just before exec() is called. You can use this to set up redirections or whatever. If code returns false no exec is performed, instead a failure is returned using the current $! value (or EINTR if $! is 0). If the fork() fails or if there is some other unexpected system error sync_exec croak()s rather than returning. sync_fhpopen_noshell fh type cmd [arg]... This is a popen() but it never invokes the shell and it uses sync_exec() under the covers. See "sync_exec". The type is either 'r' to read from the process or 'w' to write to it. The return value is the pid of the forked process. sync_popen_noshell type cmd arg... This is like sync_fhpopen_noshell, but you don't have to supply the filehandle. If called in an array context the return value is a list consisting of the filehandle and the PID of the child. In a scalar context only the filehandle is returned. sync_open fh [open-spec] This is like a Perl open() except that if a pipe is involved and the implied exec() fails sync_open() fails with $! set appropriately. See "sync_exec". Like sync_exec, sync_open croak()s if there is an unexpected system error (such as a failed pipe()). Also like sync_exec, if you use a command which Perl needs to use the shell to interpret you'll only know if the exec of the shell worked. Use sync_fhpopen_noshell or sync_exec to be sure that this doesn't happen. AUTHOR
Roderick Schertler <roderick@argon.org> SEE ALSO
perl(1). perl v5.8.8 2005-02-04 SyncExec(3pm)
All times are GMT -4. The time now is 08:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy