suspension of long process

Michael R. Wayne wayne at teemc.UUCP
Fri Oct 5 00:31:34 AEST 1990


In article <3940 at ruuinf.cs.ruu.nl> wgsiemel at praxis.cs.ruu.nl (Willem Siemelink) writes:
>I have got a process that takes days to complete.  However, the System
>Administration does not want me to run it in daytime.  So now I am looking for
>a way to stop a process and later continue it.  We have HP UX 7.0 running on
>the workstations here.
>I can do this by hand by typing ^Z on the running process followed by 'bg' and
>'fg' but that is only when I'm on the keyboard at the very moment.  Obviously
>that isn't good enough.  I've had a suggestion using 'kill' but I couldn't
>figure it out.  ('kill -3 <pid> gives a core-dump but I can't get it started
>again.)

	Follows a shar file which contains a program doing exactly what
you want to do without relying on BSD job control at all.  A couple of
commands to cron will allow you to start and stop your process on
command.  You might wish to remove the printf's once you understand what
is going on (or you might not).  Commands can alternately be placed into
your .login and .logout to run jobs only when you are not logged in.

/\/\ \/\/


#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  signal_tst.c
# Wrapped by wayne at teemc.tmc.mi.org on Thu Oct  4 10:25:48 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f signal_tst.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"signal_tst.c\"
else
echo shar: Extracting \"signal_tst.c\" \(1525 characters\)
sed "s/^X//" >signal_tst.c <<'END_OF_signal_tst.c'
X#include <stdio.h>
X#include <sys/signal.h>
X#include <time.h>
X
Xint resume(), suspend();
Xlong t;
X
Xchar *ctime();
Xlong time();
X
X/*
X * Simple program to demonstrate use of user signals to suspend
X * and resume a job which tends to run for a long time.
X *
X * Note that this really isn't a true "checkpoint" as the program
X * status is not preserved, execution is merely interrupted.
X *
X * To use:
X *	compile w/ cc
X *	run the executable in the background
X *	% kill -USR2 will suspend execution
X *	% kill -USR1 will resume execution
X */
Xmain()
X{
X	/*
X	 * Grab the signals
X	 */
X	if (((int) signal(SIGUSR1, resume) == -1) ||
X		((int) signal(SIGUSR2, suspend) == -1) ||
X		((int) signal(SIGHUP, SIG_IGN) == -1)) {
X		printf("can't catch signals.\n");
X		exit(-1);
X		}
X	/*
X	 * Normally we want processes that run for a long time to be
X	 * very, very nice.
X	 */
X	if (nice(19) == -1) {
X		time(&t);
X		printf("$.24s - can't be nice.  continuing...\n", ctime(&t));
X		}
X	(void) time(&t);
X	printf("%.24s - STARTED\n", ctime(&t));
X	/*
X	 * Your code that runs a long time goes here
X	 */
X	for(;;)
X		;	/* Loop forever for example purposes	*/
X}
X
Xresume(i)
Xint i;
X{
X	if ((int) signal(SIGUSR1, resume) == -1) {
X		printf("can't recatch signal USR1 .\n");
X		exit(-1);
X		}
X	(void) time(&t);
X	printf("%.24s - RESUMED.\n", ctime(&t));
X	return(0);
X}
X
Xsuspend(i)
Xint i;
X{
X	if ((int) signal(SIGUSR2, suspend) == -1) {
X		printf("can't recatch signal USR2.\n");
X		exit(-1);
X		}
X	(void) time(&t);
X	printf("%.24s - SUSPENDED.\n", ctime(&t));
X	pause();
X	return(0);
X}
END_OF_signal_tst.c
if test 1525 -ne `wc -c <signal_tst.c`; then
    echo shar: \"signal_tst.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0
-- 
Michael R. Wayne      ---     TMC & Associates      --- wayne at teemc.tmc.mi.org
         Operator of the only 240 Horsepower UNIX machine in Michigan 



More information about the Comp.unix.internals mailing list