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!!
-
^M is CTRL-m ... ASCII 13 ... CR ... carriage return ... it is an end-of-line markerjsotola– jsotola2021年08月12日 01:12:43 +00:00Commented Aug 12, 2021 at 1:12
-
not Raspberry Pi specific ... your question is about Linuxjsotola– jsotola2021年08月12日 01:15:24 +00:00Commented Aug 12, 2021 at 1:15
-
@jsotola it is related to Raspbian (or Raspberry OS), maybe I'm facing some problem related to hardware, don't know. So need to worry about that end of line maker, right?Luis Lange– Luis Lange2021年08月12日 01:41:15 +00:00Commented Aug 12, 2021 at 1:41
-
why would you worry about the ^M? ... it is there when you type any command in a console and press return ... the log shows the complete commandjsotola– jsotola2021年08月12日 01:51:00 +00:00Commented Aug 12, 2021 at 1:51
-
Get one simple script working, then extend that setup to your existing scripts.Dmitry Grigoryev– Dmitry Grigoryev2021年08月27日 08:53:13 +00:00Commented Aug 27, 2021 at 8:53
2 Answers 2
I can't figure out what is "^M" at the end of each line.
It's a symbol that means CR-LF - carriage return & line feed - which is the Windows method of terminating a line. Unix & Linux use only the linefeed.
That extraneous CR may be the cause of your problem - or it might be just sloppy-looking, but in either case, it should be corrected. Find the source of these bogus CRs, and eliminate them. The easiest way to do this may be with the dos2unix
utility, but you'll have to install it.
I cannot and will not debug your program for you, but I will guarantee you this:
If it runs from your command line, it can also run from
cron
.
Please note the emphasis on the word "can".
Like most things, getting your program to do what you requires that you understand the environment in which it runs. There are several differences between cron
, and your shell. In addition to differences in the PATH
environment variable (mentioned elsewhere) that must be addressed, cron
can not communicate directly to stdout
; in fact, stdout
is /dev/null
in cron
.
Finally, if debugging your code were my job, I think I would use an iterative approach: begin with something simple & gradually add complexity. Make sure you understand what's happening, and why. Add complexity after you've got the simple code working.
-
Solved! I created a text file with cron table, used dos2unix do correct it and uploaded to cron. Also run dos2unix on all my scripts and now it is working properly. Thank you for your advise, SeamusLuis Lange– Luis Lange2021年08月12日 19:59:59 +00:00Commented Aug 12, 2021 at 19:59
-
@LuisLange: Glad to hear it's working for you. Please read this.Seamus– Seamus2021年08月13日 00:08:07 +00:00Commented Aug 13, 2021 at 0:08
This is like the hundreds of similar questions on this site which you should study.
There are (at least) 2 problems.
You need fully fully qualified paths and under systemd the networking services may not have started when cron runs.
-
I changed to absolute path some days ago without success. The point is why I have a scheduled task to run at 9:10h, for example, and at 9:09h I run exactly the same script manually on the shell and the results are different. Or even if I wait until cron task runs and run manually same script results are different also.Luis Lange– Luis Lange2021年08月12日 00:53:04 +00:00Commented Aug 12, 2021 at 0:53