Sponsored Content
Top Forums Shell Programming and Scripting executing a script for a certain amount of time Post 28859 by Perderabo on Wednesday 25th of September 2002 01:15:58 PM
Old 09-25-2002
I'm a little disappointed that the OP didn't bother to deny breaking our rules by posting a homework question.

But this turns out to be much harder than it first appears. I had a hard time doing this right and I must suspect that the OP's instructor underestimated the problem. So I am going to bend our rules a bit and offer a few hints on how to approach this.

If the subsidiary script is a script that never invokes another process, never forks and simply uses shell built-ins, this would be a very trivial problem. But if the subsidiary script invokes other processes, we really need to stop and restart all those processes in one fell swope. This implies working with process groups instead of processes. And ksh, the shell I used, has very limited support for process groups. In fact, I believe that I am relying on undocumented behavior of ksh. But I have tested this under HP-UX and it is working.

To get the master script to even employ process groups I had to do:
set -o monitor
early in the master script. Turning the monitor option on and off is really intended for interactive shells. I seemed to get away with it in a script. Most of the job control stuff did not work very well in non-interactive mode. I was hoping that I just just use the job control commands in a script, but there were too many problems with them. But with monitor mode set, the shell put background jobs in a seperate process group and it set the process group id to the process id returned in the $! parameter. I expected this behavior, but I can't find it documented on the ksh man page.

But now I had both the pid and process group id of the background process. The kill command can take a negative number as the second argument. If it is not -1, it is taken as a process group id and the signal is sent to all members of the process group.

The signal SIGSTOP will cause a process to suspend. And the signal SIGCONT will cause a stopped process to resume.

When the subsidiary process finishes, the process group will persist until all members of the process group have exited. At that point, a kill command target to the now defunct process group will fail. A script can check the return code fro the kill command to detect when this happens.

This should be enough clues to complete the homework assignment. Have fun... I did! Smilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Overlapping(executing time) jobs in crontab

I do not have the luxery of a server that i can try on, so i had to post my qn here. Say i have two jobs in the cron table, the jobs are scheduled 2 mins apart. Assuming the first jobs does a lot of operations and takes > 2 mins. Will the second job be executed? Will Unix actually have a queue... (3 Replies)
Discussion started by: new2ss
3 Replies

2. Shell Programming and Scripting

need inputs on how i can change my script to reduce amount of time the script takes

HI , I have a list1 which consists of data that i have to search and a list2 which has the files that need to be searched .So basically i am using list1 on list2 to see if list1 data is present if found replace it .I have written the code using foreach loop for each list .This is taking the... (1 Reply)
Discussion started by: madhul2002
1 Replies

3. Shell Programming and Scripting

[bash] getopts not executing when called second time.

Hi, Unexpectedly, the function below doesn't seem to work when called a second time. The output shows that when the function is called the first time, it works as expected but when it is called a second time, the loop that processes the options passed to the function, with the while loop,... (2 Replies)
Discussion started by: ASGR
2 Replies

4. Programming

Date time problem while executing perl script.

Hi All, This Monday 15th March 2010, i have faced a weired issue with my Perl script execution, this script is scheduled to run at 1 minute past midnight on daily basis ( 00:01 EST ) generally for fetching previous business date , say if it is Monday it should give last Friday date, for Tuesday... (0 Replies)
Discussion started by: ravimishra
0 Replies

5. Shell Programming and Scripting

Determine amount of time to process

Hello all, Hopefully someone can point me in the right direction... I have a script written in bash which is pretty basic and just stop/starts various services based on particular conditions. What I am trying to build is a reporting type function which will send out an email with various stats... (3 Replies)
Discussion started by: systrex
3 Replies

6. Shell Programming and Scripting

executing with system time

hi, i have written an shell script which reads an folder and write the filenames to another file: here is the script: #!/bin/bash find /var/www/vhosts/remixland.de/web_users/stream001/jingle15 -type f -name "*.mp3" > /etc/shoutcast001/example.lst killall -USR1 sc_trans_linux001... (1 Reply)
Discussion started by: maydo
1 Replies

7. UNIX for Advanced & Expert Users

Kill a process after a certain amount of time

I would like to kill a process after a certain amount of time. Can I please get some ideas on how to do this? (9 Replies)
Discussion started by: cokedude
9 Replies

8. Shell Programming and Scripting

Executing multiple commands in a file at same time

Hi Am having file.ksh as below wc -l file1.txt wc -l file2.txt wc -l file3.txt wc -l file4.txt i want all the commands in this file to execute in same time please help Thanks in advance (1 Reply)
Discussion started by: ragu.selvaraj
1 Replies

9. Shell Programming and Scripting

Run a command for specific amount of time with an auto key press

Hi, I have been trying to do a small fun project for myself. I want to run a command for 45 seconds. And to get the final output of this command, the script requires I push the "q" key on my keyboard and then the final output file becomes available. I tried the following script. But it... (12 Replies)
Discussion started by: jacobs.smith
12 Replies

10. Shell Programming and Scripting

Executing bash file with sudo for the second time, leads to permission denied, for some commands

I have a script that checks if the script has been ran with sudo. If the script is not ran as sudo, the current script is being executed with exec sudo bash. You are asked for a password, you type in the password, success. Everything is perfect - the commands inside the script are ran as sudo.... (1 Reply)
Discussion started by: boqsc
1 Replies
KILL(2) 						      BSD System Calls Manual							   KILL(2)

NAME
kill -- send signal to a process LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <signal.h> int kill(pid_t pid, int sig); DESCRIPTION
The kill() function sends the signal given by sig to pid, a process or a group of processes. sig may be one of the signals specified in sigaction(2) or it may be 0, in which case error checking is performed but no signal is actually sent. This can be used to check the valid- ity of pid. For a process to have permission to send a signal to a process designated by pid, the real or effective user ID of the receiving process must match that of the sending process or the user must have appropriate privileges (such as given by a set-user-ID program or the user is the super-user). A single exception is the signal SIGCONT, which may always be sent to any descendant of the current process. If pid is greater than zero: sig is sent to the process whose ID is equal to pid. If pid is zero: sig is sent to all processes whose process group ID is equal to the process group ID of the sender, and for which the process has permission; this is a variant of killpg(3). If pid is -1: If the user has super-user privileges, the signal is sent to all processes excluding system processes and the process sending the signal. If the user is not the super user, the signal is sent to all processes with the same uid as the user excluding the process sending the signal. No error is returned if any process could be signaled. For compatibility with System V, if the process number is negative but not -1, the signal is sent to all processes whose process group ID is equal to the absolute value of the process number. This is a variant of killpg(3). RETURN VALUES
Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and errno is set to indicate the error. ERRORS
kill() will fail and no signal will be sent if: [EINVAL] sig is not a valid signal number. [ESRCH] No process can be found corresponding to that specified by pid. [ESRCH] The process id was given as 0 but the sending process does not have a process group. [EPERM] The sending process is not the super-user and its effective user id does not match the effective user-id of the receiving process. When signaling a process group, this error is returned if any members of the group could not be signaled. SEE ALSO
getpgrp(2), getpid(2), sigaction(2), killpg(3), signal(7) STANDARDS
The kill() function is expected to conform to ISO/IEC 9945-1:1990 (``POSIX.1''). BSD
April 19, 1994 BSD
All times are GMT -4. The time now is 02:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy