Nov 13, 2014

Unix Interview Questions and Answers for Java developers

Most commercial Java applications run in a Unix environment. For example, batch jobs. Here are a few Unix questions and answers to test your Unix knowledge.

Q. Can you write a Unix function that parses the command-line arguments?

For example:

 process_txns.ksh -env $ENV -backdate 20130125

A. Usually only nine command line arguments can be accessed using positional parameters. The shift command gives access to command line arguments greater than nine by shifting each of the arguments. Successive shift commands make additional arguments available. The second argument ($2) becomes the first ($1), the third ($3) becomes the second ($2) and so on.

function f_parse_command_line_args
 # Parse the command line arguments
 while [[ $# -gt 0 ]]
  case $1 in

Q.  How do you check for successful completion or failed processing of a batch file?
A. You need to use the exit status command. For example,

if [ $? -ne 0 ]
     echo "Processing has failed"
     echo "Processed successfully."

Q. How will you capture the run time stamp?
A. vRunTimestamp=`date +%Y-%m-%d-%H:%M:%S`

Q. How will you execute a Java command? say for a spring batch job?
A. For example, basically a number of -D arguments, -classpath, spring CommandLineJobRunner, spring context file (my-app-job.xml), bean id for the job myAppJob), job paramters (e.g. RUN_DATE) and log file name (${LOGFILE})

$JAVA_HOME/bin/java  -Dds.password.file=${datasourcePasswordFile} \
 ${MY_APP_FOLDER}/java/prop/keystore.jks \ \
                     -classpath ${vClaspath} \
                     my-app-job.xml \
                     myAppJob RUN_DATE=${vRunTimestamp}  \
                     >> ${LOGFILE} 2>&1

The password files are used for database connectivity and RESTful web service calls. The trusstore is used for SSL connectivity to a web service.

Q. If you want to run your job periodically say every 15 minutes, 7 days of the week, how will you achieve it
A. Put your shell script that kick off the job as a cron job.

To view all the cron jobs, type

crontab -l

To edit the cron job, type

crontab -e

You can add the cron job as

5,20,35,50 * * * *   . $HOME/.profile;  ${MYAPP_BASE_DIR}/scripts/my_app_batch.ksh

Runs 5, 20, 35, and 50 past every hour for 7 days. If you want it to run only from monday to friday, then

5,20,35,50 * * * 1,2,3,4,5   . $HOME/.profile;  ${MYAPP_BASE_DIR}/scripts/my_app_batch.ksh

0 or 7 -- Sunday, 1 - Monday, 2 - Tuesday, 3 - Wednesday, 4 - Thursday, 5- Friday, 6 - Saturday



Post a Comment

Subscribe to Post Comments [Atom]

<< Home