Below is an example of a simplified bjobs command. This program displays all pending jobs belonging to all users.
/******************************************************
* Submit command as an lsbatch job with no options set
* It is similar to the "bjobs" command with no options.
******************************************************/
#include <stdio.h>#include <lsf/lsbatch.h>#include "submit_cmd.h"int main(int argc, char **argv){/* variables for simulating submission */struct submit req; /* job specifications */memset(&req, 0, sizeof(req)); /* initializes req */struct submitReply reply; /* results of job submission */int jobId; /* job ID of submitted job *//* variables for simulating bjobs command */int options = PEND_JOB; /* the status of the jobswhose info is returned */char *user="all"; /* match jobs for all users */struct jobInfoEnt *job; /* detailed job info */int more; /* number of remaining jobsunread *//* initialize LSBLIB and get the configurationenvironment */if (lsb_init(argv[0]) < 0) {lsb_perror("simbjobs: lsb_init() failed");exit(-1);}/* check if input is in the right format:* "./simbjobs COMMAND ARGUMENTS" */if (argc < 2) {fprintf(stderr, "Usage: simbjobs command\n");exit(-1);}jobId = submit_cmd(&req, &reply, argc, argv);/* submit a job */if (jobId < 0) /* if job submissionfails, lsb_submitreturns -1 */switch (lsberrno) {/* and sets lsberrno to indicate the error */case LSBE_QUEUE_USE:case LSBE_QUEUE_CLOSED:lsb_perror(reply.queue);exit(-1);default:lsb_perror(NULL);exit(-1);}/* gets the total number of pending job. Exits if failure */if (lsb_openjobinfo(0, NULL, user, NULL, NULL, options)<0) {lsb_perror("lsb_openjobinfo");exit(-1);}/* display all pending jobs */printf("All pending jobs submitted by all users:\n");for (;;) {job = lsb_readjobinfo(&more); /* get the job details */if (job == NULL) {lsb_perror("lsb_readjobinfo");exit(-1);}printf("%s",ctime(&job->submitTime));/* submission time of job */printf("Job <%s> ", lsb_jobid2str(job->jobId));/* job ID */printf("of user <%s>, ", job->user);/* user that submits the job */printf("submitted from host <%s>\n", job->fromHost); /* name of sumbission host *//* continue to display if there is remaining job */if (!more)/* if there are no remaining jobs undisplayed,exits */break;}/* when finished to display the job info, close theconnection to the mbatchd */lsb_closejobinfo();exit(0);}
The above program will produce output similar to the following:
All pending jobs submitted by all users:Mon Mar 1 10:34:04 EST 1996Job <123> of user <john>, submitted from host <orange>Mon Mar 1 11:12:11 EST 1996Job <126> of user <john>, submitted from host <orange>Mon Mar 1 14:11:34 EST 1996Job <163> of user <ken>, submitted from host <apple>Mon Mar 1 15:00:56 EST 1996Job <199> of user <tim>, submitted from host <pear>
Use lsb_pendreason(), to print out the reasons why the job is still pending See lsb_pendreason(3) for details.