How to save command output to variable inside script?

I want do execute (via ssh) external command reading file write time, get its output and save to variable

- |      
  DT_BEFORE=$(ssh ${USER}@${SERVER} "stat -c %Y ${TARGET_FILE_CHECK} 2> /dev/null")
  echo $DT_BEFORE
  <skipped actions>
  DT_AFTER=$(ssh ${USER}@${SERVER} "stat -c %Y ${TARGET_FILE_CHECK} 2> /dev/null")
  if [ "$DT_AFTER" != "$DT_BEFORE" ]; then ssh ${USER}@${SERVER} "sudo systemctl restart ${TARGET_SERVICE}"; else echo "SKIP restart server - nothing changed"; fi

It doesnt work:

ERROR: Job failed: exit status 1

How to do that?

Hey codelans,

Is there a reason you are using a multiline block for these statements?

If you are only executing the one command stat -c %Y ${TARGET_FILE_CHECK} 2> /dev/null on the remote host followed by other actions, should this not be individual lines in your script: stanza?

Alternatively, Job failed: exit status 1 usually implies something in the execution of your job exited with an error code