0

I wrote a Cron Job and it doesn't run. Any Idea why it doesn't run?

This is the Cron Job I expect to run:

#
#
#                   (day of month)                (day of week)
#  min   hou    dom (day of month)    mon     dow (day of week)    command
   *     *      *                     *       *                    /usr/bin/touch /DATA/z_test_$(date '+%Y-%m-%d_T%H-%M').txt >/dev/null 2>&1

It's supposed to run every minute.
I want it to create an empty file and save it in the /DATA/ directory.

This is the root of my file system:

Tue Apr 11 [srv_data@Ora1 /home/michal]$ ls -lah /
total 28K
dr-xr-xr-x.  18 root     root      236 Apr  3 10:52 .
dr-xr-xr-x.  18 root     root      236 Apr  3 10:52 ..
lrwxrwxrwx.   1 root     root        7 Oct  9  2021 bin -> usr/bin
dr-xr-xr-x.   5 root     root     4.0K Mar 27 11:22 boot
drwxrwx--x.   2 srv_data srv_data 4.0K Apr 11 14:51 DATA
drwxr-xr-x.  18 root     root     3.2K Apr  4 12:20 dev
drwxr-xr-x. 115 root     root     8.0K Apr  5 14:37 etc
drwxr-xr-x.   4 root     root       36 Apr  6 08:58 home
lrwxrwxrwx.   1 root     root        7 Oct  9  2021 lib -> usr/lib
lrwxrwxrwx.   1 root     root        9 Oct  9  2021 lib64 -> usr/lib64
drwxr-xr-x.   2 root     root        6 Oct  9  2021 media
drwxr-xr-x.   2 root     root        6 Oct  9  2021 mnt
drwxr-xr-x.   2 root     root        6 Oct  9  2021 opt
dr-xr-xr-x. 304 root     root        0 Apr  4 12:20 proc
dr-xr-x---.   3 root     root      163 Apr 11 12:04 root
drwxr-xr-x.  39 root     root     1.1K Apr  4 12:20 run
lrwxrwxrwx.   1 root     root        8 Oct  9  2021 sbin -> usr/sbin
drwxr-xr-x.   2 root     root        6 Oct  9  2021 srv
dr-xr-xr-x.  13 root     root        0 Apr  4 12:20 sys
drwxrwxrwt.   6 root     root     4.0K Apr 11 15:14 tmp
drwxr-xr-x.  13 root     root      158 Mar 20 13:08 usr
drwxr-xr-x.  22 root     root     4.0K Mar 28 08:54 var

I'm running this Cron Job as user 'srv_data' and this user has the permissions to create and save files in the /DATA/ directory.

I have also tried to take this command and run it from user 'srv_data' and it works. It creates the file in the location I expect it to.

Tue Apr 11 [srv_data@Ora1 /home/michal]$ ls -lah /DATA
total 20K
drwxrwx--x.  2 srv_data srv_data 4.0K Apr 11 15:31  .
dr-xr-xr-x. 18 root     root      236 Apr  3 10:52  ..
-rwxrwx--x.  1 srv_data srv_data 1.7K Apr  4 10:14  blade_runner_the_movie_1982
-rw-r--r--.  1 srv_data srv_data  960 Apr 11 15:00  file_with_dates
-rwxrwx--x.  1 srv_data srv_data 2.2K Apr  4 10:09  mud_the_movie_2012
-rw-r--r--.  1 root     root        0 Apr 11 12:43  reczny_plik_01
-rw-r--r--.  1 root     root        0 Apr 11 12:43  reczny_plik_02
-rw-r--r--.  1 root     root        0 Apr 11 12:43  reczny_plik_03
-rw-r--r--.  1 root     root        0 Apr 11 12:43  reczny_plik_04
-rw-r--r--.  1 root     root        0 Apr 11 12:45  reczny_plik_05
-rw-r--r--.  1 root     root        0 Apr 11 12:45  reczny_plik_06
-rw-r--r--.  1 root     root        0 Apr 11 12:45  reczny_plik_07
-rw-r--r--.  1 root     root        0 Apr 11 12:45  reczny_plik_08
-rw-r--r--.  1 root     root        0 Apr 11 12:45  reczny_plik_09
-rwxrwx--x.  1 srv_data srv_data   66 Apr  3 11:18  synch_test_00
-rw-rw-r--.  1 srv_data srv_data    0 Apr  7 15:06  test_2023-04-07_T1506
-rw-rw-r--.  1 srv_data srv_data    0 Apr  7 15:14  test_2023-04-07_T1514
-rw-rw-r--.  1 srv_data srv_data    0 Apr  7 15:17  test_2023-04-07_T1517
-rw-rw-r--.  1 srv_data srv_data    0 Apr  7 15:22  test_2023-04-07_T15-22.txt
-rw-rw-r--.  1 michal   michal      0 Apr 11 14:46  z_test_2023-04-11_T14-46.txt
-rw-rw-r--.  1 michal   michal      0 Apr 11 14:51  z_test_2023-04-11_T14-51.txt
-rw-rw-r--.  1 srv_data srv_data    0 Apr 11 15:31  z_test_2023-04-11_T15-31.txt

Here is the contents of the /var/log/cron file.

Apr 11 14:54:01 Ora1 CROND[22557]: (srv_data) CMD (/usr/bin/touch /DATA/z_test_$(date '+)
Apr 11 14:54:01 Ora1 CROND[22559]: (srv_data) CMD (/home/srv_data/sync.sh)
Apr 11 14:54:01 Ora1 CROND[22543]: (srv_data) CMDOUT (/bin/sh: -c: line 0: unexpected EOF while looking for matching `'')
Apr 11 14:54:01 Ora1 CROND[22543]: (srv_data) CMDOUT (/bin/sh: -c: line 1: syntax error: unexpected end of file)
Apr 11 14:54:02 Ora1 CROND[22542]: (srv_data) CMDOUT (sending incremental file list)
Apr 11 14:54:02 Ora1 CROND[22542]: (srv_data) CMDOUT ()
Apr 11 14:54:02 Ora1 CROND[22542]: (srv_data) CMDOUT (sent 602 bytes  received 12 bytes  409.33 bytes/sec)
Apr 11 14:54:02 Ora1 CROND[22542]: (srv_data) CMDOUT (total size is 4,970  speedup is 8.09)
Apr 11 14:55:01 Ora1 CROND[22586]: (srv_data) CMD (/home/srv_data/sync.sh)
Apr 11 14:55:01 Ora1 CROND[22584]: (srv_data) CMD (/usr/bin/touch /DATA/z_test_$(date '+)
Apr 11 14:55:01 Ora1 CROND[22570]: (srv_data) CMDOUT (/bin/sh: -c: line 0: unexpected EOF while looking for matching `'')
Apr 11 14:55:01 Ora1 CROND[22570]: (srv_data) CMDOUT (/bin/sh: -c: line 1: syntax error: unexpected end of file)
Apr 11 14:55:01 Ora1 CROND[22569]: (srv_data) CMDOUT (sending incremental file list)
Apr 11 14:55:01 Ora1 CROND[22569]: (srv_data) CMDOUT ()
Apr 11 14:55:01 Ora1 CROND[22569]: (srv_data) CMDOUT (sent 602 bytes  received 12 bytes  1,228.00 bytes/sec)
Apr 11 14:55:01 Ora1 CROND[22569]: (srv_data) CMDOUT (total size is 4,970  speedup is 8.09)
Apr 11 14:56:01 Ora1 CROND[22611]: (srv_data) CMD (/home/srv_data/sync.sh)
Apr 11 14:56:01 Ora1 CROND[22612]: (srv_data) CMD (/usr/bin/touch /DATA/z_test_$(date '+)
Apr 11 14:56:01 Ora1 CROND[22597]: (srv_data) CMDOUT (/bin/sh: -c: line 0: unexpected EOF while looking for matching `'')
Apr 11 14:56:01 Ora1 CROND[22597]: (srv_data) CMDOUT (/bin/sh: -c: line 1: syntax error: unexpected end of file)
Apr 11 14:56:02 Ora1 CROND[22596]: (srv_data) CMDOUT (sending incremental file list)
Apr 11 14:56:02 Ora1 CROND[22596]: (srv_data) CMDOUT ()
Apr 11 14:56:02 Ora1 CROND[22596]: (srv_data) CMDOUT (sent 602 bytes  received 12 bytes  409.33 bytes/sec)
Apr 11 14:56:02 Ora1 CROND[22596]: (srv_data) CMDOUT (total size is 4,970  speedup is 8.09)
Apr 11 14:57:01 Ora1 CROND[22641]: (srv_data) CMD (/home/srv_data/sync.sh)
Apr 11 14:57:01 Ora1 CROND[22639]: (srv_data) CMD (/usr/bin/touch /DATA/z_test_$(date '+)
Apr 11 14:57:01 Ora1 CROND[22625]: (srv_data) CMDOUT (/bin/sh: -c: line 0: unexpected EOF while looking for matching `'')
Apr 11 14:57:01 Ora1 CROND[22625]: (srv_data) CMDOUT (/bin/sh: -c: line 1: syntax error: unexpected end of file)
Apr 11 14:57:01 Ora1 CROND[22624]: (srv_data) CMDOUT (sending incremental file list)
Apr 11 14:57:01 Ora1 CROND[22624]: (srv_data) CMDOUT ()
Apr 11 14:57:01 Ora1 CROND[22624]: (srv_data) CMDOUT (sent 602 bytes  received 12 bytes  1,228.00 bytes/sec)
Apr 11 14:57:01 Ora1 CROND[22624]: (srv_data) CMDOUT (total size is 4,970  speedup is 8.09)
Apr 11 14:58:01 Ora1 CROND[22670]: (srv_data) CMD (/usr/bin/touch /DATA/z_test_$(date '+)
Apr 11 14:58:01 Ora1 CROND[22669]: (srv_data) CMD (/home/srv_data/sync.sh)
Apr 11 14:58:01 Ora1 CROND[22655]: (srv_data) CMDOUT (/bin/sh: -c: line 0: unexpected EOF while looking for matching `'')
Apr 11 14:58:01 Ora1 CROND[22655]: (srv_data) CMDOUT (/bin/sh: -c: line 1: syntax error: unexpected end of file)
Apr 11 14:58:02 Ora1 CROND[22654]: (srv_data) CMDOUT (sending incremental file list)
Apr 11 14:58:02 Ora1 CROND[22654]: (srv_data) CMDOUT ()
Apr 11 14:58:02 Ora1 CROND[22654]: (srv_data) CMDOUT (sent 602 bytes  received 12 bytes  409.33 bytes/sec)
Apr 11 14:58:02 Ora1 CROND[22654]: (srv_data) CMDOUT (total size is 4,970  speedup is 8.09)
Apr 11 14:59:01 Ora1 CROND[22698]: (srv_data) CMD (/home/srv_data/sync.sh)
Apr 11 14:59:01 Ora1 CROND[22699]: (srv_data) CMD (/usr/bin/touch /DATA/z_test_$(date '+)
Apr 11 14:59:01 Ora1 CROND[22683]: (srv_data) CMDOUT (/bin/sh: -c: line 0: unexpected EOF while looking for matching `'')
Apr 11 14:59:01 Ora1 CROND[22683]: (srv_data) CMDOUT (/bin/sh: -c: line 1: syntax error: unexpected end of file)
Apr 11 14:59:01 Ora1 CROND[22682]: (srv_data) CMDOUT (sending incremental file list)
Apr 11 14:59:01 Ora1 CROND[22682]: (srv_data) CMDOUT ()
Apr 11 14:59:01 Ora1 CROND[22682]: (srv_data) CMDOUT (sent 602 bytes  received 12 bytes  1,228.00 bytes/sec)
Apr 11 14:59:01 Ora1 CROND[22682]: (srv_data) CMDOUT (total size is 4,970  speedup is 8.09)

But I can't read this cron file log. I understand nothing. What is CMD and CMDOUT in the 5th column of this file??

What /bin/sh: -c: means??

  • 4
    % character has a special meaning, see also https://unix.stackexchange.com/a/48005/30851 – frostschutz Apr 11 '23 at 14:01
  • Just a stab in the dark, but wouldn't it be "/DATA/z_test_date '+%Y-%m-%d_T%H-%M'.txt"? I replaced the "$(..)" with "....". – dev4life Apr 11 '23 at 14:03
  • 1
    @dev4life No -- I think OP wants the process substitution to be part of the filename, so there is a new unique file created each minute. – Paul_Pedant Apr 11 '23 at 15:33
  • 1
    Cron runs each command in a shell wrapper (default shell is /bin/sh). The wrapper is like sh -c ' your command ' and CMD is the log marker for that command. CMDOUT seems to be stderr from that command -- for your job, syntax errors. Those are caused by the % in the date specifier -- every one of those needs escaping like \%. man -s 5 crontab explains that -- search the man page for Percent. – Paul_Pedant Apr 11 '23 at 15:46
  • @Paul_Pedant that I know. I was saying to substitute the $ for the "`" character. I guess stackoverflow replaced my text with the gray highlighting. – dev4life Apr 11 '23 at 16:26
  • @dev4life OK, the highlighting misled me. But the back-tick notation for command substitution is "old-style" now, largely because they don't nest well. – Paul_Pedant Apr 11 '23 at 20:02
  • Can anyone of the commenters write if my use of z_test_$(date '+%Y-%m-%d_T%H-%M').txt is old style / deprecated and how should it be written, according to the up-to-date standards. Please. – michal roesler Apr 12 '23 at 07:27
  • https://unix.stackexchange.com/questions/29578/how-can-i-execute-date-inside-of-a-cron-tab-job – michal roesler Apr 19 '23 at 06:53

0 Answers0