class SystemCommandTasklet implements StepExecutionListener, StoppableTasklet, InitializingBean {

	protected static final Log logger = LogFactory.getLog(SystemCommandTasklet.class);

	private CommandRunner commandRunner = new JvmCommandRunner();

	private String[] cmdArray;

	private String[] environmentParams = null;

	private File workingDirectory = null;

	private SystemProcessExitCodeMapper systemProcessExitCodeMapper = new SimpleSystemProcessExitCodeMapper();

	private long timeout = 0;

	private long checkInterval = 1000;

	private StepExecution execution = null;

	private TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();

	private boolean interruptOnCancel = false;

	private volatile boolean stopped = false;

	private JobExplorer jobExplorer;

	private boolean stoppable = false;

	/**
	 * Execute system command and map its exit code to {@link ExitStatus} using
	 * {@link SystemProcessExitCodeMapper}.
	 */
	@Nullable
	@Override
	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

		FutureTask<Integer> systemCommandTask = new FutureTask<>(() -> {
			Process process = commandRunner.exec(cmdArray, environmentParams, workingDirectory);
			return process.waitFor();
		});

		long t0 = System.currentTimeMillis();

		taskExecutor.execute(systemCommandTask);

		while (true) {
			Thread.sleep(checkInterval);// moved to the end of the logic

			if (stoppable) {
				JobExecution jobExecution = jobExplorer
					.getJobExecution(chunkContext.getStepContext().getStepExecution().getJobExecutionId());

				if (jobExecution.isStopping()) {
					stopped = true;
				}
			}

			if (systemCommandTask.isDone()) {
				Integer exitCode = systemCommandTask.get();
				ExitStatus exitStatus = systemProcessExitCodeMapper.getExitStatus(exitCode);
				contribution.setExitStatus(exitStatus);
				if (ExitStatus.FAILED.equals(exitStatus)) {
					throw new SystemCommandException("Execution of system command failed with exit code " + exitCode);
				}
				else {
					return RepeatStatus.FINISHED;
				}
			}
			else if (System.currentTimeMillis() - t0 > timeout) {
				systemCommandTask.cancel(interruptOnCancel);
				throw new SystemCommandException("Execution of system command did not finish within the timeout");
			}
			else if (execution.isTerminateOnly()) {
				systemCommandTask.cancel(interruptOnCancel);
				String command = String.join(" ", cmdArray);
				throw new JobInterruptedException("Job interrupted while executing system command '" + command + "'");
			}
			else if (stopped) {
				systemCommandTask.cancel(interruptOnCancel);
				contribution.setExitStatus(ExitStatus.STOPPED);
				return RepeatStatus.FINISHED;
			}
		}
	}

	/**
	 * Injection setter for the {@link CommandRunner}.
	 * @param commandRunner {@link CommandRunner} instance to be used by
	 * SystemCommandTasklet instance. Defaults to {@link JvmCommandRunner}.
	 * @since 5.0
	 */
	public void setCommandRunner(CommandRunner commandRunner) {
		this.commandRunner = commandRunner;
	}

	/**
	 * Set the command to execute along with its arguments. For example:
	 *
	 * <pre>setCommand("myCommand", "myArg1", "myArg2");</pre>
	 * @param command command to be executed in a separate system process.
	 */
	public void setCommand(String... command) {
		this.cmdArray = command;
	}

	/**
	 * @param envp environment parameter values, inherited from parent process when not
	 * set (or set to null).
	 */
	public void setEnvironmentParams(String[] envp) {
		this.environmentParams = envp;
	}

	/**
	 * @param dir working directory of the spawned process, inherited from parent process
	 * when not set (or set to null).
	 */
	public void setWorkingDirectory(String dir) {
		if (dir == null) {
			this.workingDirectory = null;
			return;
		}
		this.workingDirectory = new File(dir);
		Assert.isTrue(workingDirectory.exists(), "working directory must exist");
		Assert.isTrue(workingDirectory.isDirectory(), "working directory value must be a directory");

	}

	@Override
	public void afterPropertiesSet() throws Exception {<FILL_FUNCTION_BODY>}

	public void setJobExplorer(JobExplorer jobExplorer) {
		this.jobExplorer = jobExplorer;
	}

	/**
	 * @param systemProcessExitCodeMapper maps system process return value to
	 * <code>ExitStatus</code> returned by Tasklet.
	 * {@link SimpleSystemProcessExitCodeMapper} is used by default.
	 */
	public void setSystemProcessExitCodeMapper(SystemProcessExitCodeMapper systemProcessExitCodeMapper) {
		this.systemProcessExitCodeMapper = systemProcessExitCodeMapper;
	}

	/**
	 * Timeout in milliseconds.
	 * @param timeout upper limit for how long the execution of the external program is
	 * allowed to last.
	 */
	public void setTimeout(long timeout) {
		this.timeout = timeout;
	}

	/**
	 * The time interval how often the tasklet will check for termination status.
	 * @param checkInterval time interval in milliseconds (1 second by default).
	 */
	public void setTerminationCheckInterval(long checkInterval) {
		this.checkInterval = checkInterval;
	}

	/**
	 * Get a reference to {@link StepExecution} for interrupt checks during system command
	 * execution.
	 */
	@Override
	public void beforeStep(StepExecution stepExecution) {
		this.execution = stepExecution;
	}

	/**
	 * Sets the task executor that will be used to execute the system command NB! Avoid
	 * using a synchronous task executor
	 * @param taskExecutor instance of {@link TaskExecutor}.
	 */
	public void setTaskExecutor(TaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}

	/**
	 * If <code>true</code> tasklet will attempt to interrupt the thread executing the
	 * system command if {@link #setTimeout(long)} has been exceeded or user interrupts
	 * the job. <code>false</code> by default
	 * @param interruptOnCancel boolean determines if process should be interrupted
	 */
	public void setInterruptOnCancel(boolean interruptOnCancel) {
		this.interruptOnCancel = interruptOnCancel;
	}

	/**
	 * Will interrupt the thread executing the system command only if
	 * {@link #setInterruptOnCancel(boolean)} has been set to true. Otherwise the
	 * underlying command will be allowed to finish before the tasklet ends.
	 *
	 * @since 3.0
	 * @see StoppableTasklet#stop()
	 */
	@Override
	public void stop() {
		stopped = true;
	}

}

		Assert.state(commandRunner != null, "CommandRunner must be set");
		Assert.state(cmdArray != null, "'cmdArray' property value must not be null");
		Assert.state(!ObjectUtils.isEmpty(cmdArray), "'cmdArray' property value is required with at least 1 element");
		Assert.state(StringUtils.hasText(cmdArray[0]), "'cmdArray' property value is required with at least 1 element");
		Assert.state(systemProcessExitCodeMapper != null, "SystemProcessExitCodeMapper must be set");
		Assert.state(timeout > 0, "timeout value must be greater than zero");
		Assert.state(taskExecutor != null, "taskExecutor is required");
		stoppable = jobExplorer != null;
	
<no_super_class>