Running cron for nodejs and deleting files in temp - linux

I'm trying to run nodejs /var/www/html/back/elastic/users.js g command every 10 minutes in cron but I don't seem to be able to
*/10 * * * * /usr/local/bin nodejs /var/www/html/back/elastic/users.js
I've added this to crontab -e but when I check syslog it doesn't show there.
Same for the following command I want to delete files in temp every day, it doesn't work either
30 2 * * * rm -rf /var/www/html/data/users/temp/*
What am I missing? Thanks for any help
Ubuntu Server 15.04

Try:
*/10 * * * * /usr/local/nodejs /var/www/html/back/elastic/users.js
and
30 2 * * * /bin/rm -rf /var/www/html/data/users/temp/*

You should try removing the space from the first crontab
*/10 * * * * /usr/local/bin/nodejs /var/www/html/back/elastic/users.js
the second command looks correct to me, is the path correct? Maybe a permission issue.
It should delete everything under /var/www/html/data/users/temp/ at 2:30am

Related

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.

I can't get crontab to run, sudo python3 to run script an issue?

I have been researching this topic for the past two hours and can't find similar info. I am putting the last touch on a LED sign and I want it to run the script every x amount of minutes from raspberry to update the info going to the sign, lets just say every 10 minutes. I have tried everything with crontab -e and sudo crontab. my question is I have to run the file (mysign.py) from the directory in cd my_python and then from there I have to use the command sudo python3 mysign.py, it will not run with sudo python. I am wondering if this has anything to do with it?
here's some of what I have tried, along with the #reboot as well with nothing.
/10 * * * * /usr/bin/python mysign.py
/10 * * * * /usr/bin/python3 mysign.py
/10 * * * * /usr/bin/python /home/pi/my_python/mysign.py
/10 * * * * /home/pi/my_python/mysign.py
First of all, to execute on every 10th minute you need to use */10 ... not /10 ....
Second, entries from root's crontab execute as root, hence their home is not /home/pi - you actually need to specify the whole path for both the interpreter and the script:
*/10 * * * * /usr/bin/python3 /home/pi/my_python/mysign.py
Make sure you set it in the root's crontab (sudo crontab -e).
This, of course, assumes the location of your python3 interpreter and the script itself, if those paths are not correct - correct them before adding to crontab.

Why is my crontab not working?

I'm trying to get a cron job to run rsync. I started with this:
*/30 * * * * rsync -avz -e "ssh -i /home/ubuntu/ocf_dev_us" ubuntu#10.0.12.76:/home/ubuntu/kumar/ /home/ubuntu/kumar/"
and it wasn't working, so I replaced it with this:
0 * * * * env > /tmp/env.output
and even that isn't working. How do I find out what's going on?
Your cronjob fires only every 60 minutes. Try this
* * * * * env > /tmp/env.output
to find out if your cronjob is working.

Can't make crontab work

I am new to Linux and Ubuntu and I seldom have to use it. I am trying to make this PHP script to run every minute using cron, but firstly I wanted to make some tests.
I created an empty file at /var/www/html/ called test. I ran on terminal:
sudo crontab -e
And added this line:
0 * * * * rm /var/www/html/test
Then saved it and exited. It said "Installing new Crontab"
Nothing happened. Then I created a file bfile.sh that contained:
#!/bin/sh
rm /var/www/html/test
and added the following to crontab:
0 * * * * bash /var/www/html/bfile.sh
Still nothing happened.
What do I have to do to see anything happening from crontab? By the way I checked and the service is running
0 * * * * basically says "run this at 0th minute of every hour."
If you need cron to run your command every minute do * * * * *.
0 * * * * runs once every 1 hour. If you want to run every minute it should be */1 * * * *
You can also check the /var/log/cron file for any errors

crontab not running command

I've been trying to run a crontab command but it isn't running for some reason. The command is supposed to send push notifications.
My sudo crontab -e looks like this:
0 0 * * 0 /home/[user]/resetWeeklyLeaderboard
* * * * * /home/[user]/pushDelivery
I have a file called pushDelivery at the location above which contains the following:
/usr/bin/flock -n /home/[user]/PushChatServer/push/lockfile usr/bin/php /home/[user]/PushChatServer/push/push.php development
I have also made pushDelivery executable by doing chmod +x pushDelivery. This code works perfectly for my resetWeeklyLeaderboard file but won't call the pushDelivery file.
It works if I run
/home/[user]/pushDelivery
It works if I run
/usr/bin/flock -n /home/[user]/PushChatServer/push/lockfile usr/bin/php /home/[user]/PushChatServer/push/push.php development
However if in crontab I do
* * * * * /home/[user]/pushDelivery
or
* * * * * /usr/bin/flock -n /home/[user]/PushChatServer/push/lockfile usr/bin/php /home/[user]/PushChatServer/push/push.php development
it doesn't work. Please help me. Thank you!
UPDATE:
It still doesn't work but I've tried more stuff. I tried reversing the order in the crontab
* * * * * /home/[user]/pushDelivery
0 0 * * 0 /home/[user]/resetWeeklyLeaderboard
it doesn't work. I also tried making my resetWeeklyLeaderboard code run minutely
* * * * * /home/[user]/pushDelivery
* * * * * /home/[user]/resetWeeklyLeaderboard
and that works for my resetWeeklyLeaderboard code but not for my pushDelivery code. This implies to me that it is something in my pushDelivery code that is causing the issue. However I run /home/[user]/pushDelivery from command line and it works. What could be causing this problem?
i dont know why is is not running but same thing happens to me before at that time insted of doing
* * * * * /home/[user]/pushDelivery
try this
*/1 * * * * /home/[user]/pushDelivery
it worked for me in this way...(both the task run in every minute)
I fixed the issue. In my pushDelivery file I was supposed to write:
/usr/bin/flock -n /home/[user]/PushChatServer/push/lockfile /usr/bin/php /home/[user]/PushChatServer/push/push.php development
whereas I'd written:
/usr/bin/flock -n /home/[user]/PushChatServer/push/lockfile usr/bin/php /home/[user]/PushChatServer/push/push.php development
The "/" before the "usr/bin/php" makes all the difference. Somehow just that slash will allow it to work outside of crontab but will fail it when run inside crontab. I don't understand why but this is the correct solution.

Resources