Why is my Crontab not running at a specific time? - python-3.x

I have the following cron running that works:
*/2 * * * * cd /toThePath && /usr/bin/env python3 /toThePath test.py
when I have this:
0 16 * * * cd /toThePath && /usr/bin/env python3 /toThePath test.py
It does not send at 16h00. I have tried 0 16 */1 */1 */1.
My script does include #!/usr/bin/env python3 in the beginning

Try running this in terminal.
date
Does the time displayed match your time zone?

Related

Running node script with crontab

I'm trying to run a node script with crontab. I've tried first doing things like
* * * * * echo test > test.txt
to be sure crontab works (I'm trying to make the command work and then I'll change the crontab to something different so it doesn't run every minute).
The crontab above works. The thing is, when I try to use node, it doesn't run with the crontab. Running which node I get /usr/bin/node Here are the things I've tried.
Thanks!
* * * * * cd /path/to/script && node script.js
* * * * * cd /path/to/project && npm start (runs npx tsc && node build/script.js)
* * * * * cd /path/to/script && node script.js > test.txt (file is generated empty, even though, script has console.log)
* * * * * node /path/to/script/script.js
* * * * * echo test > test.txt && node /path/to/script/script.js (file gets generated)
Also I've tried all of the above replacing node by /usr/bin/node.
If I run any of these commands manually, it executes the program.
After going over a long time and testing a lot of stuff, I realized the issue was doing sudo crontab -e to set the crontab. I fixed it after running instead
sudo crontab -u username -e

Run yarn script into crontab

I made a script in TypeScript that download data from some api and store inside a mongo DB.
If i run yarn start from the app folder it works well.
I would like to put this command in a cron job that will be executed every 5 minutes.
I try it with some sintax in crontab but ti doesn't work.
I try to put the call in a run.sh script but it doesn't work too.
*/5 * * * * cd /opt/app-folder/src/ && /home/username/.nvm/versions/node/v16.15.1/bin/ts-node main.ts
*/5 * * * * cd /opt/app-folder && /usr/bin/yarn start > /home/username/app-name-out.txt
*/5 * * * * /home/username/run.sh > /home/username/app-name-out.txt
*/5 * * * * /home/username/.nvm/versions/node/v16.15.1/bin/ts-node /opt/app-folder/src/main.ts > /home/username/app-name-out.txt
*/5 * * * * cd /opt/app-folder/src/ && /home/username/.nvm/versions/node/v16.15.1/bin/ts-node main.ts > /home/username/app-name-out.txt
Can someone help me to execute the main.ts every 5 minutes?
Thanks
I get rid of this problem.
There was 2 problems, the first related to the output redirection.
I fixed by redirect stdout in a file and stderr in another one.
The second was related the the $PATH of crontab: it was /usr/bin:/bin.
To fix it I log into my user where script works and I print my $PATH with echo $PATH.
I copied the value and I set it before the crontab line in crontab file.
This is what it looks like:
# Set the same path of user username to have the correct path in script
PATH=/home/username/.nvm/versions/node/v16.15.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
# Execute oracle every 5 minutes
*/5 * * * * /bin/sh /home/username/run.sh >> /home/username/app-name-info.txt 2>> /home/username/app-name-error.txt
Now it works.

How to run Scrapy-spider from crontab?

I'm trying to set a task to run a script from such a sh-file (run_a.sh)
#!/bin/bash
cd /home/userdir
source venvProject/bin/activate
cd /home/userdir/scrapy_project
PATH=$PATH:/usr/local/bin
export PATH
scrapy crawl my_spider
My crontab string looks like this:
*/5 * * * * sh /home/userdir/run_a.sh
but it don't works, whats wrong?

Cronjob won't trigger .sh script

I have made this Cronjob that should run some tests. My script works, but cronjob won't trigger it.
Cronjob looks like this:
*/1 * * * * /bin/sh cd ~/Desktop/abc.sh
I want it to run every minute, just for testing purposes.
And my script is:
while read LINE; do curl -o /dev/null --silent --head --write-out "%{http_code} $LINE\n" "$LINE"; done < todo | tee test_results.txt
I can't even find the solution on google or youtube.
If you added #!/bin/bash to your script, then your cronjob should look like:
* * * * * ~/Desktop/abc.sh
Or
* * * * * /home/USER/Desktop/abc.sh
In the first case you have to run cronjob from the same user where of the Desktop folder.

How do I execute cron service in linux?

I am trying to run a Python code every minute in Linux but at loss to configure cron service. Following is what I added after running 'crontab -e' but nothing is happening.
* * * * * cd /home/kali && /usr/bin/python3.8 /home/kali/time.py
time.py contains simple code to show current time.
from datetime import datetime
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
Any suggestions?
Firstly you need to add it to crontab,try:
sudo nano /etc/crontab
and after that you can put your service:
* * * * * cd /home/kali && /usr/bin/python3.8 /home/kali/time.py
Check it again 'sudo crontab -e'
you should run your code with python3. i dont understand why you use cd command, so i omit that.
This is the way you can run your python code in cron:
* * * * * python3 /home/kali/time.py
Note: Check if you have permission to execute .py file.

Resources