Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!samsung!umich!sharkey!cfctech!teemc!wayne From: wayne@teemc.UUCP (Michael R. Wayne) Newsgroups: comp.unix.internals Subject: Re: suspension of long process Message-ID: <175179@teemc.UUCP> Date: 4 Oct 90 14:31:34 GMT References: <3940@ruuinf.cs.ruu.nl> Reply-To: wayne@teemc.UUCP (Michael R. Wayne) Organization: TMC & Associates, Troy, MI Lines: 122 In article <3940@ruuinf.cs.ruu.nl> wgsiemel@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 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 signal_tst.c <<'END_OF_signal_tst.c' X#include X#include X#include 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