Another background server case that wasn't discussed above is that where the background process is a one-off process that runs only at specific times. Or maybe once per day (as in a nightly recap/cleanup/backup process). In this case, most of the above techniques are overkill and/or otherwise unhelpful, and you might as well just use the operating system's background scheduler.
For Linux, that would be
crontab. Here's the procedure for setting that up from within A-Shell...
1) Figure out the command line you need to launch the process. For an A-Shell program, it should be something like this:
/vm/miame/bin/ashell -i /vm/miame/miame.ini -td dumb -j bgjob bg1 > /vm/miame/dsk0/001002/bg1log.log 2>&1
Breaking that down, we have:
a)
/vm/miame/bin/ashell (the ashell executable)
b)
-i /vm/miame/miame.ini (the miame.ini file)
c)
-td dumb (this selects a teltyp-style terminal driver, which is appropriate for background tasks and will keep the captured log clean of escape sequences)
d)
-j bgjob (optional to force the jobname to "BGJOB".) Note that if such a job already exists, the new job will try to kill/remove the existing one, which may or may not be appropriate.
e)
bg1 (the command file which launches your program.) It should be in the CMD: diretory, e.g. CMD:BG1.CMD, and handle both logging in and perhaps even checking if the job is already running (as in the example given at the top of this thread).
f)
> /vm/miame/dsk0/001002/bg1log.log (redirect the screen output, i.e. stdout, to the specified file). That might be useful for debugging. If you don't need it, use
> /dev/null instead.
g)
2>&1 (redirect the stderr stream to the same place as the stdout). This would typically only consist of OS-level error messages that occur during startup, but covers all bases.
2) You can test your command line from the A-Shell prompt by preceding it with
host and adding a
& at the end so that it runs in background. Otherwise the command will hang your job as it waits, perhaps endlessly, for the background job to run. Note that you can use SYSTAT to see if the job is running, and perhaps look at the file where you redirected the output to see any messages that the process output to the screen, not to mention any log files explicitly created by the process.
.HOST /vm/miame/bin/ashell -i /vm/miame/miame.ini -td dumb -j bgjob bg1 > /vm/miame/dsk0/001002/bg1log.log 2>&1 &
.
.systat/n ; check if BG1 job running
.kill bg1 ; terminate it
3) Once you're happy with the way the process launches, set it up to run at the appointed time by creating a
crontab entry as follows:
The above will launch a vi (editor) session allowing you to edit the crontab for the current user, i.e. you. The crontab file consists of individual lines with 5 numeric fields at the start, followed by the command line you want to launch. (There may also be comment lines starting with the
# character, and vi uses the
~ character to indicate EOF lines.)
The 5 numeric fields at the start of the command line control when the command line is executed:
Field 1: minute within hour, 0-59
Field 2: hour within day: 0-23
Field 3: day of month, 1-31
Field 4: month, 1-12
Field 5: day of week, 0-6 (0=Sunday)
Each of the fields can be a single number, or a comma-delimited list of numbers, or a range (e.g. 1-5) or * (meaning not applicable). So for example, to run a command every day at 11:30 pm, you would use:
30 23 * * * command
(i.e. 23:30 every day of the month, every month, every day of the week)
To run a command every minutes every day except Sunday:
0,10,20,30,40,50 * * * 1-6 command
(There are lots of Internet references to this for more detail).
The vi editor has essentially 3 modes:
- editing
- command (cursor movement and editing commands)
- command prompt (save, search, etc.)
The commands you need to know are (everything is case sensitive!) :
i - insert (and start editing)
ESC - cancel editing or command prompt (back to the command mode)
x - deletes character at cursor
dd - deletes the entire line
: (colon) - brings up command prompt at bottom of screen
Otherwise use the arrow keys to move around, BACK for rubout. To insert a new line, position the cursor at the end of an existing one, hit
i and then ENTER and then start typing.
To save your file, hit ESC to cancel editing mode, then
: to get the command prompt, then
wq! to save (write) and quit. To quit without saving, do the same but use the
q! command (quit without first writing).
Cron typically wraps at column 80, so after entering the command line above, and setting it up to run at 10:45 PM each night, it would look like this:
45 22 * * * /vm/miame/bin/ashell -i /vm/miame/miame.ini -td dumb -j bgjob bg1 >
/vm/miame/dsk0/001002/bg1log.log 2>&1
~
~
When you save the file, crontab will verify that you haven't made any egregious errors, and if not, it will say something like:
crontab: installing new crontab
You can check the existing crontab with:
.HOST /O crontab -l
45 22 * * * /vm/miame/bin/ashell -i /vm/miame/miame.ini -td dumb -j bgjob bg1 >
/vm/miame/dsk0/001002/bg1log.log 2>&1
If you don't like the setup, you can just repeat the
crontab -e command, using the
dd command to delete entire lines, or insert
# in front to comment them out.
Once you're satisfied, sit back and let the scheduler take care of launching your program at the appointed time. You can check later to see that it ran at the right time by looking in the ashlog.log file (assuming you have TRACE=INOUT in the miame.ini, as you should).
For Windows, the concept is similar but the details are all different because you would be using the Task Scheduler, which, in inimitable Windows fashion, is slightly different in every version of Windows. (So best to just look it up online.) The A-Shell command line would be essentially the same as the UNIX case, except no need for the
> redirection, and you probably want to use the -z or -zi
Command Line Switches