Cron Job Creating Odd File

Status
Not open for further replies.

zorb56

Cadet
Joined
Sep 12, 2017
Messages
2
Hi all! So I've been trying to create a cron job to output CPU temperature and a timestamp to a text file for logging. I have the following script written, and the commands work as expected when run in the shell separately, but when run in the cron job, the output for DATE goes into a file named C3HSBO~G and the output for the core temps goes into the expected file. Any ideas as to why this would be occurring?

Code:
#!/bin/bash
TODAY=$(date)
echo $TODAY >> '/mnt/HomeNAS/shared/NAS Documentation/CPU.txt'
sysctl -a |egrep -E "cpu\.[0-9]+\.temp" >> '/mnt/HomeNAS/shared/NAS Documentation/CPU.txt'
 

Ericloewe

Server Wrangler
Moderator
Joined
Feb 15, 2014
Messages
20,194
Looks like the filename is being mangled for some reason.
 

Inxsible

Guru
Joined
Aug 14, 2017
Messages
1,123
Hi all! So I've been trying to create a cron job to output CPU temperature and a timestamp to a text file for logging. I have the following script written, and the commands work as expected when run in the shell separately, but when run in the cron job, the output for DATE goes into a file named C3HSBO~G and the output for the core temps goes into the expected file. Any ideas as to why this would be occurring?

Code:
#!/bin/bash
TODAY=$(date)
echo $TODAY >> '/mnt/HomeNAS/shared/NAS Documentation/CPU.txt'
sysctl -a |egrep -E "cpu\.[0-9]+\.temp" >> '/mnt/HomeNAS/shared/NAS Documentation/CPU.txt'
Best option is to not use spaces in unix filenames/paths. But if you have to, try using double quotes instead of single quotes around the filename. Single quotes in bash usually don't interpolate anything like $(date) for example. Double quotes will.
 

fracai

Guru
Joined
Aug 22, 2012
Messages
1,212
I can't explain what you're seeing, the weird filename does look a bit like a long file name that has been rewritten by Windows, but as Inxsible suggested, try adding some double quotes.

Code:
#!/bin/bash
TODAY="$(date)"
echo "$TODAY" >> '/mnt/HomeNAS/shared/NAS Documentation/CPU.txt'
sysctl -a |egrep -E "cpu\.[0-9]+\.temp" >> '/mnt/HomeNAS/shared/NAS Documentation/CPU.txt'


or get rid of the TODAY variable entirely. You're not using it anywhere else anyway.

Code:
#!/bin/bash
echo "$(date)" >> '/mnt/HomeNAS/shared/NAS Documentation/CPU.txt'
sysctl -a |egrep -E "cpu\.[0-9]+\.temp" >> '/mnt/HomeNAS/shared/NAS Documentation/CPU.txt'
 

Pezo

Explorer
Joined
Jan 17, 2015
Messages
60
Just a side note: egrep -E is redundant, egrep is typically aliased to grep -E.
 

zorb56

Cadet
Joined
Sep 12, 2017
Messages
2
Thanks for the responses everyone! Turns out I had a rogue control character from editing in Windows.... Rewrote the script in the shell and all is well. Yeah I'm a Linux novice. Cheers!

Thanks for the tips too
@fracai - I used the variable just to see if that would give me different results, was a troubleshooting step
@Pezo - Noted! Thanks.
 

fracai

Guru
Joined
Aug 22, 2012
Messages
1,212
Thanks for the responses everyone! Turns out I had a rogue control character from editing in Windows.... Rewrote the script in the shell and all is well. Yeah I'm a Linux novice.
Happens to all of us. I can't even tell you how many times I've spent effort debugging only to realize my editor has saved with the wrong line endings.
 
Status
Not open for further replies.
Top