I made a python code to read SMS from SIM800 module, that is connected to a Raspberry Pi Zero W, and it is working like a charm when I run it on a terminal or another IDE. Here is the expected result:
safemonitor@safemonitor:~ $ python3 sms.py
--------------------------------------------
11-08-2021 11:37:27
Conexao OK
+5511xxxxxxxxx
gsm
Leitura SMS OK
SMS recebidos: 1
Telefone: +5511xxxxxxxxx -> OK
Comando: gsm -> OK
Apagando todas as mensagens
Valida SMS OK
Modulo desconectado com sucesso
safemonitor@safemonitor:~ $
In other words, I send a SMS text to SIMCARD, my routine identifies the sender number and text sent and validate both information.
But when running under cron it identifies nothing, like this:
safemonitor@safemonitor:~ $ python3 sms.py
--------------------------------------------
11-08-2021 11:41:18
Conexao OK
Leitura SMS OK
SMS recebidos: 0
Valida SMS OK
Modulo desconectado com sucesso
safemonitor@safemonitor:~ $
Here is cron table:
# m h dom mon dow command
0-55/5 * * * * bash /home/safemonitor/scripts/aplicacao.sh
1-59/3 * * * * bash /home/safemonitor/comandos.sh
4,34 * * * * bash /home/safemonitor/scripts/console.sh
@reboot /home/safemonitor/scripts/aplicacao.sh
I have 3 scripts been called from cron and yes, first one is called every 5min, second every 3min, thrid every 4th and 34th minutes and also at boot I call the first one.
This is the script to run python code:
safemonitor@safemonitor:~ $ cat comandos.sh
#!/bin/bash
python3 /home/safemonitor/sms.py 2>&1 1>>/home/safemonitor/log/sms.log &
safemonitor@safemonitor:~ $
I also tried to run python directly from cron without success, like this:
# m h dom mon dow command
0-55/5 * * * * bash /home/safemonitor/scripts/aplicacao.sh
1-59/3 * * * * python3 /home/safemonitor/sms.py 2>&1 1>>/home/safemonitor/log/sms.log &
4,34 * * * * bash /home/safemonitor/scripts/console.sh
@reboot /home/safemonitor/scripts/aplicacao.sh
I'm not using root user, my user is safemonitor with root rights
Here is part of the syslog:
Aug 11 11:30:01 safemonitor CRON[5698]: (safemonitor) CMD (bash /home/safemonitor/scripts/aplicacao.sh ^M)
Aug 11 11:31:01 safemonitor CRON[5729]: (safemonitor) CMD (bash /home/safemonitor/comandos.sh^M)
Aug 11 11:34:02 safemonitor CRON[5783]: (safemonitor) CMD (bash /home/safemonitor/scripts/console.sh^M)
Aug 11 11:34:02 safemonitor CRON[5782]: (safemonitor) CMD (bash /home/safemonitor/comandos.sh^M)
Aug 11 11:35:01 safemonitor CRON[5814]: (safemonitor) CMD (bash /home/safemonitor/scripts/aplicacao.sh ^M)
I can't figure out what is "^M" at the end of each line.
CRON timming is working as expected, aplicacao.sh is doing what it has to do, but both, console.sh and comandos.sh, are not working.
Here is the "mail" sent from CRON:
2021-08-11 11:30 Page 1
1 From: root (Cron Daemon)
2 To: safemonitor
3 Subject: Cron <safemonitor@safemonitor> bash /home/safemonitor/scripts/aplicacao.sh
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=ISO-8859-1
6 Content-Transfer-Encoding: 8bit
7 X-Cron-Env: <SHELL=/bin/sh>
8 X-Cron-Env: <HOME=/home/safemonitor>
9 X-Cron-Env: <PATH=/usr/bin:/bin>
10 X-Cron-Env: <LOGNAME=safemonitor>
11
12 TERM environment variable not set.
13
14
15 --------------------------------------------------------------------------------
16 | V E R I F I C A S E P Y T H O N E S T Á R O D A N D O |
17 --------------------------------------------------------------------------------
18
19
20 [11-08-2021_11-30-01] Python rodando! Nada a fazer, tchau ...
21
--------------
2021-08-11 11:31 Page 1
1 From: root (Cron Daemon)
2 To: safemonitor
3 Subject: Cron <safemonitor@safemonitor> bash /home/safemonitor/comandos.sh
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=ISO-8859-1
6 Content-Transfer-Encoding: 8bit
7 X-Cron-Env: <SHELL=/bin/sh>
8 X-Cron-Env: <HOME=/home/safemonitor>
9 X-Cron-Env: <PATH=/usr/bin:/bin>
10 X-Cron-Env: <LOGNAME=safemonitor>
11
: No such file or directoryitor/comandos.sh
--------------
2021-08-11 11:34 Page 1
1 From: root (Cron Daemon)
2 To: safemonitor
3 Subject: Cron <safemonitor@safemonitor> bash /home/safemonitor/comandos.sh
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=ISO-8859-1
6 Content-Transfer-Encoding: 8bit
7 X-Cron-Env: <SHELL=/bin/sh>
8 X-Cron-Env: <HOME=/home/safemonitor>
9 X-Cron-Env: <PATH=/usr/bin:/bin>
10 X-Cron-Env: <LOGNAME=safemonitor>
11
: No such file or directoryitor/comandos.sh
--------------
2021-08-11 11:34 Page 1
1 From: root (Cron Daemon)
2 To: safemonitor
3 Subject: Cron <safemonitor@safemonitor> bash /home/safemonitor/scripts/console.sh
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=ISO-8859-1
6 Content-Transfer-Encoding: 8bit
7 X-Cron-Env: <SHELL=/bin/sh>
8 X-Cron-Env: <HOME=/home/safemonitor>
9 X-Cron-Env: <PATH=/usr/bin:/bin>
10 X-Cron-Env: <LOGNAME=safemonitor>
11
: No such file or directoryitor/scripts/console.sh
--------------
2021-08-11 11:35 Page 1
1 From: root (Cron Daemon)
2 To: safemonitor
3 Subject: Cron <safemonitor@safemonitor> bash /home/safemonitor/scripts/aplicacao.sh
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=ISO-8859-1
6 Content-Transfer-Encoding: 8bit
7 X-Cron-Env: <SHELL=/bin/sh>
8 X-Cron-Env: <HOME=/home/safemonitor>
9 X-Cron-Env: <PATH=/usr/bin:/bin>
10 X-Cron-Env: <LOGNAME=safemonitor>
11
12 TERM environment variable not set.
13
14
15 --------------------------------------------------------------------------------
16 | V E R I F I C A S E P Y T H O N E S T Á R O D A N D O |
17 --------------------------------------------------------------------------------
18
19
20 [11-08-2021_11-35-01] Python rodando! Nada a fazer, tchau ...
21
--------------
2021-08-11 11:37 Page 1
1 From: root (Cron Daemon)
2 To: safemonitor
3 Subject: Cron <safemonitor@safemonitor> bash /home/safemonitor/comandos.sh
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=ISO-8859-1
6 Content-Transfer-Encoding: 8bit
7 X-Cron-Env: <SHELL=/bin/sh>
8 X-Cron-Env: <HOME=/home/safemonitor>
9 X-Cron-Env: <PATH=/usr/bin:/bin>
10 X-Cron-Env: <LOGNAME=safemonitor>
11
: No such file or directoryitor/comandos.sh
--------------
First thing is that the mail is truncating last line, don't know why:
( : No such file or directoryitor/scripts/console.sh and : No such file or directoryitor/comandos.sh )
To get this mail output I created a executable file at /usr/sbin/sendmail with this content:
#!/bin/bash
exec >> /tmp/sendmail.$(whoami) 2>&1
pr -Fn
echo --------------
This is the hole story. Hope I could provide all information.
Any idea is very, very welcome!!