Search code examples
symlinkmainframezosjcl

Replacing target of symlink with newest subfolder using zOS script


Every time I deploy an agent on zOS server, a new subfolder is created under /usr/local/avi/ .

I manually change an existing symlink to that subfolder, the newest one, after each deployment.

I want to write a job that would check which is the newest subfolder under /usr/local/avi and update the symlink accordingly.

I have almost no knowledge of zOS.

Thanks


Solution

  • By "... want to write a job...", are you talking about a z/OS batch job? If so, you can run prgram BPXBATCH to run the shell commands or shell script you would run manually on a shell session.

    //jobname JOB CLASS=....  <== You need to adapt this JOB statement to fit your installations needs.
    //STEP01   EXEC PGM=BPXBATCH
    //STDOUT   DD   SYSOUT=*
    //STDERR   DD   SYSOUT=*
    //STDENV   DD   *
    SYMLINK=sym-link-to-current-dir
    DIRPREFIX=deployment-directory-prefix
    /*
    //STDPARM  DD   *
    SH
      cd /target/directory ;                                         
      rm $SYMLINK ;                                             
      ln -s  $(ls -trd $DIRPREFIX* | tail -n 1)  $SYMLINK ;  
    /*
    

    Above commands assume that the name of the deployment directories have a common prefix. Adjust variable DIRPREFIX under //STDENV accordingly. For convenience, the symlink name is also set as variable (SYMLINK).

    Note The shell pipe symbol | is a variant character. That means the actual character to be typed in the TSO/ISPF session depends on the code page you're using. For example, I'm running with CP500 (Multinational), and I have to type an exclamation point ! for the shell to see the |.

    Running a batch job implies that you are allowed to submit the job, and that you are allowed to read the jobs output, e.g. via SDSF in your TSO/ISPF session.

    There is no means to automatically detect directory creation and fire up some script, as far as I can tell.

    Some details about the UNIX commands run:

    1. The cd /target/directory should be self-explanatory

    2. The rm $SYMLINK removes the symlink, so that it can be created again pointing the the new directory.

    3. The next line ln -s $(ls -trd $DIRPREFIX* | tail -n 1) $SYMLINK creates a new symlink to the newest directory. In this line, the part $(.....) runs a subshell and returns the resulting output (stdout) back to the parent shell, which replaces the $(.....) with the result, and then continues to execute the command line.

    The ls -trd $DIRPREFIX* lists the names of all matching directories (*1) and sorts the output by time (-t) in reverse order (-r), so that the newest directory will be listed in the last line. This output is then piped to the tail -n 1 command which list only the last line (-n 1). This is line is then replacing the $(.....) to complete the ln utility.

    (*1) Actually the ls lists all directory entries matching the $DIRPREFIX* operand, not only directories. But for the current problem, it is assumed that there are no other matching entries but (deployment) directory entries.