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
Related
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
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
Things that changed recently on my server:
I'm almost sure it's because the dist-upgrade.(few days ago)
I added a new user and added him a library in var/www/html/banana.
so it might be from that too (?) - (2 weeks ago)
Tried installing FastCGI without any success - but this didn't disrupt any regular processing and flow. (2 months ago)
I usually run API queries from my PHP code using forking, and in some point it stopped working for me (it does work, but when getting to heavy query results it stopps).
error.log:
[Sun Aug 28 12:15:03.201994 2016] [:notice] [pid 1882] FastCGI: process manager initialized (pid 1882)
[Sun Aug 28 12:15:03.278176 2016] [mpm_prefork:notice] [pid 1879] AH00163: Apache/2.4.18 (Ubuntu) mod_fastcgi/mod_fastcgi-SNAP-0910052141 configured -- resuming normal operations
running cat /var/mail/root outputs:
From root#banana Sun Aug 28 12:39:01 2016
Return-Path: <root#banana>
X-Original-To: root
Delivered-To: root#banana
Received: by banana (Postfix, from userid 0)
id ABC281005BA; Sun, 28 Aug 2016 12:39:01 +0300 (IDT)
From: root#banana (Cron Daemon)
To: root#banana
Subject: Cron <root#banana> [ -x /usr/lib/php/sessionclean ] && /usr/lib/php/sessionclean
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: <20160828093901.ABC281005BA#YHserver>
Date: Sun, 28 Aug 2016 12:39:01 +0300 (IDT)
Can someone help me debug the problem better and solve it?
Running this script gets true results:
<?php
echo "Is fork? <br/>";
var_dump (extension_loaded('pcntl'));
echo "<br><br> more checks: <br>";
$supports = array();
if (function_exists("pcntl_fork")) $supports[] = "ispcntl";
echo implode(",", $supports);
for ($i = 1; $i <= 5; ++$i) {
$pid = pcntl_fork();
if (!$pid) {
sleep(1);
print "In child $i\n";
exit;
}
}
?>
EDIT: I Tried running that same script on the server without forking and I got all the results right (after waiting a lot and getting my website stuck for a while..)
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
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