Command not Found: Cron [duplicate] - bash

This question already has answers here:
Getting "command not found" error in bash script
(6 answers)
CronJob not running
(19 answers)
Closed 2 years ago.
When running the below command,
* * * * * /home/path1/path2/myScript.sh >>/home/path1/path2/Logs/output.txt 2>&1
I got the following error
bash: apache-solr-1.4.2-dev.zip: command not found
// if we delete this folder/zip from its location it goes to the below error.
// It is just taking some folder names and throwing errors
bash: apache-solr-1.4.2.zip: command not found
bash: apache-solr-1.4.zip: command not found
bash: someFolder.zip: command not found
bash: someFolder2.zip: command not found
bash: someFolder3.zip: command not found
myScript.sh contains:
echo "this is my script"
I can't understand the relation between Solr and Cron job. Please help me out.

To configure cron, you'll have to edit the users cronfile. You can usually do this by doing crontab -e logged in as the actual users. This will launch $EDITOR and let you edit the current cron file.
You'll need to enter the cron configuration into this file, and not just pasting it on the command line as you're doing.
* * * * * /home/path1/path2/myScript.sh >>/home/path1/path2/Logs/output.txt 2>&1
The error messages you're getting is from the *s being expanded into the actual contents of the directory you're inside.

I've run the cron job command directly instead of creating/editing the cron job. After ruuning the command using
crontab -e
it worked fine.
Thanks for replies.

The cron job does nothing else than to start myScript.sh at certain times. I think you get the error message because you are referring to that apache-solr-1.4.2-dev.zip or to something that the apache-solr-1.4.2-dev.zip does from within the script myScript.sh.
So, the error message has to do with the script and not with the cron job. To find the cause for the error, you will have to fix the script.

Related

How to run sacct command in shell script

I am trying to write a cron that will run sacct command of slurm for given dates and save it in file. As I don't have much experience with shell script I am not sure how to do it.
I did the following:
I created a shell script with the following code (sacct_data.sh):
#!/bin/bash
startdate=`date +"%Y-%m-%dT00:00:00"`
enddate=`date -d "yesterday" '+%Y-%m-%dT00:00:00'`
TZ=UTC sacct info_that_needs_to_be_pulled --starttime $startdate --endtime $enddate > data.log
In the crontab I have the following code:
* * * * * bash sacct_data.sh #I know this will run every min, but that's is not important
However, I am getting the error "sacct: command not found".
Any help is appreciated :)
The sacct: command not found error means that the command was not found in the PATH. And that is expected as the PATH set in cron environments is really minimal. You can either set a correct PATH variable (see this for instance) or use the absolute sacct path: type which sacct and use the output in sacct_data.sh.

Shell script doesn't run properly while running from crontab

I read the other related topics but they didn't help me.
I have a shell script which checks if my python script is not running,it will run it. Otherwise it will just skip and do nothing.
It totally works when I use:
bash myshellscrip.sh
And I get the result that I want which is doing some tasks and sending emails to some correspondents. However, when I try to run this particular shell script on crontab, it doesn't send out the emails and doesn't do the other tasks.
I tried the following on crontab and none of them worked.
* * * * * /bin/bash /path/to/my/script/myshellscrip.sh
* * * * * /bin/bash /path/to/my/script/myshellscrip.sh >> /some/other/path/output.txt
When I save the changes into 'output.txt' file, it creates the file but it doesn't send the emails or doing other tasks.
I also tried the option of reboot because I need this program to run at start up too, and this didn't work:
#reboot /bin/bash /path/to/my/script/myshellscrip.sh
Does anyone know how to fix it?
EDIT:
As I was checking with the simplest shell scrip like:
#!/bin/sh
/usr/bin/python /home/pi/DCA/code.py
My crontab wouldn't have any output in my output.txt file although my code.py have something printing out, too.
However, when I use a very simple python code for example only a 'print' statement it will run and save the output into output.txt.
Seems like your shell script crashes / stops before it can do something (possibly due to the environment being different or permission issues). You can check /var/log/syslog to find out.
You could try removing /bin/bash, I don't think that's necessary?
Run the cron job in debug mode. for that, Add -x to the bash command on the cronjob and save their output in the file.
bash -x /path/to/script.sh >> /path/to/the/output.txt
You can find the problem.
Apparently crontab was running my script several times. So I tried to use different locking mechanisms to put a lock around my scrip but only using flock worked for me. In my crontab I added this line:
* * * * * /usr/bin/flock -n /tmp/ms.lockfile /bin/bash /path/to/my/script/myShellScript.sh

Cron job does not run, but command is OK when run manually at shell

I have the following entry in my crontab:
0,30 7-18 * * 1-5 cd /path/to/scrapers && scrapy crawl funny_quotes &>> $(date "+/home/foobar/logs/\%Y\%m\%d.funny.log"
This entry is supposed to run every half hour, on weekdays and append the output to the log file each time it's run. I have tested the syntax online, using this handy tool, and the syntax is correct.
However, the job doesn't get run. What's worse, the log file is created (but has no contents - file size 0), so I have no diagnostic information to go by.
The command cd /path/to/scrapers && scrapy crawl funny_quotes runs perfectly when I type it at the command, and there is copious amounts of information output to the console, from scrapy.
Why does the cronjob fail to run sccessfully - and why is nothing being piped to the log file?
Check your cron logs
grep CRON /var/log/syslog
I am sure you are getting error something like scrapy - command not found or something similar.
To fix it, do this
Enter and copy the output of echo $PATH from shell.
And then open crontab -e
At the very top of file, write PATH=YOUR_COPIED_CONTENTS
And that should work.

Cron job does not run

Following is the entry in the crontab:
MAILTO=abc#gmail.com
45 14 * * * /home/user/simple.sh
I've also done chmod +x on the simple.sh But the crontab does not run, it doesn't even send an email.
pgrep cron shows an Id. I also tried bouncing crond. But no luck!
Could someone please point out the mistake here
The simple.sh script is:
#! /bin/bash
echo hello
Thanks
Since you are doing a echo within the cron job script, you need to capture its output somewhere.
Your shebang and file mode (using chmod +x) are all right, so those aren't the issue here and running without /bin/sh should work fine.
Try using the following to see the output in cron.log file (This runs every minute)
* * * * * /home/user/simple.sh >> /home/user/cron.log
Note that cron jobs run in separate subprocess shell, with reduced environment, so its output won't be visible on your terminal.
Regarding sending of email - you need to have some mail package (like postman, mutt etc) configured for the cron daemon to send out error mails.
Do not use relative paths, but absolute ones. Also, indicate the binary running the script, that is /bin/sh (or whatever coming from which sh):
45 14 * * * /bin/sh /path/to/script/simple.sh
Maybe there shouldn't be a space in line 1 of your .sh script:
#! /bin/bash
to
#!/bin/bash
Although I could see why it would still seem to work from when invoked in an interactive shell (# could merely comment out the rest of the line).
Still, I'd guess at worst it'd merely ignore that line and inherit cron's interpreter of /bin/sh

Bash Script with Crontab

I'm trying to setup a crontab I have this in my current job in the current user I'm logged into
* * * * * /CS/day/get_info.sh
get_info.sh is supposed to output a text file every minute and I suspected that it would output a file in the same directory as the script is located but it doesn't.
I've also checked the syslogs to see if I could figure this out.
(user) CMD (/CS/day/get_info.sh)
(user) MAIL (mailed 46 bytes of output but got status 0x0001#012)
Can someone explain to me why this is happening?
Thanks
man cron tells you:
When executing commands, any output is mailed to the owner of the
crontab (or to the user named in the MAILTO environment variable in the
crontab, if such exists). The children copies of cron running these
processes have their name coerced to uppercase, as will be seen in the
syslog and ps output.
So you have to
cd into the appropriate directory yourself (cron will use $HOME)
redirect ANY output to a file of your choice
You can do both things in the crontab. But I recommend to do it in the first lines of the script itself:
#!/bin/bash
cd WHEREEVER_YOU_WANT
exec > YOUR_LOG_FILE 2&>1
The script is run in the home directory of the user and the file should be there as well. If you want it in the same directory as the script, either do a cd in your script or modify your crontab entry:
*/1 19-20 * * * cd /CS/day; /CS/day/get_info.sh
Another common problem with crontab entries is the environment. If the script works correctly in your terminal, try debugging it, when it is run from cron:
40 11 * * * bash -x /CS/day/get_info.sh >/tmp/get_info.sh.log 2>&1
Run it once only with current time, because otherwise you will overwrite your log file every minute.
On my case, I just had to install and configure an smtp client.

Resources