crontab script fail: end of file unexpected (expecting ")") when call $(date) - bash

I want to add
0 5 1 * * goaccess -f /var/log/nginx/access.log -a > /home/xan/reports/report-week-$(date +%Y.%m.%d).html
but crontab always complains about that:
Subject: Cron <root#deimos> goaccess -f /var/log/nginx/access.log -a > /home/xan/reports/report-week-$(date +
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
Message-Id: <E1bIogT-0001FX-9n#deimos>
Date: Fri, 01 Jul 2016 05:00:01 +0200
/bin/sh: 1: Syntax error: end of file unexpected (expecting ")")
What the proper syntax to do that?

% has special meaning in a crontab (it represents a newline), so you need to escape it to specify a literal percent sign.
0 5 1 * * goaccess ... > /home/xan/reports/report-week-$(date +\%Y.\%m.\%d).html

Related

unbound variable when run a crontab task in mac os x

when I run script in mac os x like this:
*/1 * * * * /Users/dolphin/Library/"Mobile Documents"/com~apple~CloudDocs/Document/source/dolphin/dolphin-scripts/bash/cron/latex_compile_alive_monitor.sh >> /Users/dolphin/shell.log
the output is:
From dolphin#dolphins-MacBook-Pro.local Tue Jan 21 20:47:01 2020
Return-Path: <dolphin#dolphins-MacBook-Pro.local>
X-Original-To: dolphin
Delivered-To: dolphin#dolphins-MacBook-Pro.local
Received: by dolphins-MacBook-Pro.local (Postfix, from userid 501)
id 7430417C4C19; Tue, 21 Jan 2020 20:47:00 +0800 (CST)
From: dolphin#dolphins-MacBook-Pro.local (Cron Daemon)
To: dolphin#dolphins-MacBook-Pro.local
Subject: Cron <dolphin#dolphins-MacBook-Pro> /Users/dolphin/Library/"Mobile Documents"/com~apple~CloudDocs/Document/source/dolphin/dolphin-scripts/bash/cron/latex_compile_alive_monitor.sh >> /Users/dolphin/shell.log
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=dolphin>
X-Cron-Env: <USER=dolphin>
X-Cron-Env: <HOME=/Users/dolphin>
Message-Id: <20200121124701.7430417C4C19#dolphins-MacBook-Pro.local>
Date: Tue, 21 Jan 2020 20:47:00 +0800 (CST)
+ BOOK_PATH='/Users/dolphin/Library/Mobile Documents/com~apple~CloudDocs/Document/source/dolphin/summary/'
+ COMMAND='/Library/TeX/texbin/latexmk -pdfxe -pvc -xelatex -interaction=nonstopmode '
+ PROCESS_NAME_KEYWORDS_MAP=(["dolphin-book-2020.tex"]="${COMMAND} ./dolphin-book-2020/dolphin-book-2020.tex" ["the-book-of-mine.tex"]="${COMMAND} ./the-books-of-mine/the-book-of-mine.tex" ["kubelet-learn.tex"]="${COMMAND} ./kubelet-learn/kubelet-learn.tex")
/Users/dolphin/Library/Mobile Documents/com~apple~CloudDocs/Document/source/dolphin/dolphin-scripts/bash/cron/latex_compile_alive_monitor.sh: line 15: dolphin: unbound variable
which variable was unboud? COMMAND? I am already defined in script.this is my script:
#!/usr/bin/env bash
# 当使用未初始化的变量时,程序自动退出
set -u
# 当任何一行命令执行失败时,自动退出脚本
set -e
# 在运行结果之前,先输出执行的那一行命令
set -x
BOOK_PATH="/Users/dolphin/Library/Mobile Documents/com~apple~CloudDocs/Document/source/dolphin/summary/"
COMMAND="/Library/TeX/texbin/latexmk -pdfxe -pvc -xelatex -interaction=nonstopmode "
declare -A PROCESS_NAME_KEYWORDS_MAP=(
["dolphin-book-2020.tex"]="${COMMAND} ./dolphin-book-2020/dolphin-book-2020.tex"
["the-book-of-mine.tex"]="${COMMAND} ./the-books-of-mine/the-book-of-mine.tex"
["kubelet-learn.tex"]="${COMMAND} ./kubelet-learn/kubelet-learn.tex"
)
cd "${BOOK_PATH}"
for key in ${!PROCESS_NAME_KEYWORDS_MAP[#]}
do
PID_COUNT=`ps -ef | grep "${key}" | grep -v "grep" | wc -l`
if [[ ${PID_COUNT} -lt 1 ]]; then
nohup `${PROCESS_NAME_KEYWORDS_MAP[${key}]}` &
else
echo "process already exists..."
fi
done
maybe remove the space in the end of COMMAND

$HOME not expanding in cron file even though it is HOME is shown as an environmental variable

I got the following error from executing a line in my Vixie Cron file:
/bin/sh: 1: cd: can't cd to $HOME/prg/gridtrader
Which to me means that $HOME is not being expanded even though crontab(5) says it will be and the log from execution shows HOME as being defined.
The cron file looks like so:
GT_DIR = $HOME/prg/gridtrader
PYTHON_EXE = $HOME/install/python37/bin/python3
APPLICATION = gridtrader/gridtrader.py
ACCOUNT = metaperl_yobit
# GRID TRADER
*/5 * * * * cd $GT_DIR && $PYTHON_EXE $APPLICATION --monitor $ACCOUNT
The log message I got is:
X-Cron-Env: <ST=/home/schemelab/prg/surgetrader/src>
X-Cron-Env: <INVOKE=/home/schemelab/install/miniconda3/bin/invoke>
X-Cron-Env: <I=iniset_1>
X-Cron-Env: <AGT=~/prg/adsactly-gridtrader>
X-Cron-Env: <GT_DIR=$HOME/prg/gridtrader>
X-Cron-Env: <PYTHON_EXE=$HOME/install/python37/bin/python3>
X-Cron-Env: <APPLICATION=gridtrader/gridtrader.py>
X-Cron-Env: <ACCOUNT=metaperl_yobit>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/schemelab>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=schemelab>
Date: Tue, 26 Feb 2019 11:25:01 -0500 (EST)
/bin/sh: 1: cd: can't cd to $HOME/prg/gridtrader
As you can see the, from the shell, there is no problem executing this cd command:
schemelab#metta:~$ ls /home/schemelab/prg/gridtrader
LICENSE SPEC.md accounts doc log requirements.txt system.ini tmp venv
README.md StatusReport.docx batch gridtrader persistence src system.ini.sample tradelog.docx
schemelab#metta:~$ cd /home/schemelab/prg/gridtrader
schemelab#metta:~/prg/gridtrader$ ls -ld ~/prg/gridtrader
drwxr-xr-x 13 schemelab schemelab 4096 Feb 18 13:56 /home/schemelab/prg/gridtrader
schemelab#metta:~/prg/gridtrader$
You have run into one of cron's most common problems, trying to use it like an arbitrary shell script. Cron is not a shell script and you can't do everything you can do in one, like dereferencing variables or setting arbitrary new variables.
I suggest you replace your values into the cron line and avoid usage of variables:
# GRID TRADER
*/5 * * * * cd $HOME/prg/gridtrader && $HOME/install/python37/bin/python3 gridtrader/gridtrader.py --monitor metaperl_yobit
Another approach to the problem of unexpanded cron variable content is eval:
*/5 * * * * eval cd $GT_DIR && eval $PYTHON_EXE $APPLICATION --monitor $ACCOUNT

Linux CRON: unexpected EOF while looking for matching ``' [duplicate]

This question already has an answer here:
/bin/sh: 1: Syntax error: EOF in backquote substitution
(1 answer)
Closed 4 years ago.
I am running this command on cron:
0 4 1 * * myfunc `date +%Y-%m`; anotherfunc
on my mail file /var/spool/mail/myuser, I get this message:
From root#myserver.localdomain Thu Nov 1 04:00:01 2018
Return-Path: <root#myserver.localdomain>
X-Original-To: myuser
Delivered-To: daniele#myserver.localdomain
Received: by myserver.localdomain (Postfix, from userid 500)
id 5F3A081CF9; Thu, 1 Nov 2018 04:00:01 +0100 (CET)
From: root#myserver.localdomain (Cron Daemon)
To: myuser#myserver.localdomain
Subject: Cron <myuser#myserver> myfunc `date +
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/myuser>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=myuser>
X-Cron-Env: <USER=myuser>
Message-Id: <20181101030001.5F3A081CF9#myserver.localdomain>
Date: Thu, 1 Nov 2018 04:00:01 +0100 (CET)
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
what am I doing wrong?
I found the reason: it's because % should be escaped with \
the correct version would be:
0 4 1 * * myfunc `date +\%Y-\%m`; anotherfunc
https://unix.stackexchange.com/questions/29578/how-can-i-execute-date-inside-of-a-cron-tab-job

my job disappears without trace

I am running a weather forecast model and I want to automate the modek runs via crontab. When I submit my job interactive through:
qsub -I -l nodes=8:ppn=8 -l walltime=2:00:00
The job runs ok.
When I submit through crontab, the job disappears without any trace, no error file any where, no output file, except mail which reads as:
From: root#master.cluster (Cron Daemon)
To: test#master.cluster
Subject: Cron <test#master> PATH=/opt/torque/bin:/usr/bin:/bin:. qsub /home/test
/WRF/SCRIPTS/wrf_00_run.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/test>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=test>
X-Cron-Env: <USER=test>
Message-Id: <20141220080001.D0F6B5C08ED#master.cluster>
Date: Sat, 20 Dec 2014 10:00:01 +0200 (CAT)
152.master.cluster
Here is my crontab entry
50 09 * * * PATH=/opt/torque/bin:/usr/bin:/bin:. qsub /home/test/WRF/SCRIPTS/wrf_submit_00.sh
and wrf_submit_00.sh has two dependent jobs to run consecutively
#!/bin/bash
cd WRF/WRFV3/run
FIRST =`qsub wrf_initialise.sh`
echo $FIRST
SECOND = `qsub -w depend=afterok:$FIRST wrf_00_run.sh`
echo $SECOND
exit 0
and the first job is
#!/bin/bash
#PBS -l nodes=8:ppn=8
cd WRF/WRFV3/run
echo -n "this script is running on: "
hostname -f
date
echo ""
echo "### PBS_NODEFILE (${PBS_NODEFILE}) ###"
cat ${PBS_NODEFILE}
echo ""
mpirun ./real.exe
exit 0
When I submit the two jobs separately through crontab I get the same disappearance.
Please help!, am stuck. Google search of a similar problem gives me no clue.

CRONTAB syntax error

Herer is my CRONTAB file (Ubuntu 10.10):
57 1 * * 2-6 ET=`date --date 'yesterday'+%Y%m%d`;echo $ET
Even The syntax color indicate that something is wrong. and there is this error:
Subject: Cron <root> ET=`date --date 'yesterday' + (failed)
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
/bin/sh: Syntax error: EOF in backquote substitution
But I am not sure whats wrong. Thanks a lot!
Cron needs to escape the % sign - http://www.hcidata.info/crontab.htm
Try it with a backslash:
57 1 * * 2-6 ET=`date --date 'yesterday' +\%Y\%m\%d`;echo $ET

Resources