Running a global node module binary from crontab - node.js

I'm trying to run a node module (npm install -g lungo-cli) from within my crontab.
What I've tried is to call the node binary from crontab:
* * * * * lungo
No luck.
Then I tried with:
* * * * * /usr/local/bin/lungo
Again, no luck.
So finally I did what I thought it was a workaround, calling the node module directly from a javascript file using shelljs.
Javascript:
const shell = require('shelljs')
shell.exec('lungo')
I even tried running it locally!
const lungo = require('lungo-cli/bin/lungo')
const shell = require('shelljs')
const shell.exec('lungo')
Crontab:
* * * * * /usr/local/bin/node $HOME/scripts/lungo.js
And it's the same, it's not working, it seems like crontab is running in a completely different environment than my zsh shell and can't find any binaries other than the OSX specific ones like cat, rm, cd and so on.
I'm using Mac OSX 10.14.1 (Mojave) and zsh as my shell.
How could I accomplish this? I want to be able to call my node cli programs with crontab.

I'm using Oh-my-zsh in my macbook so I've tried many things to get the crontab task runs but finally, my solution was prepending the .zshrc before the command to run.
*/30 * * * * . $HOME/.zshrc; node /path/for/my_script.js
This task runs every 30 minutes and uses .zshrc profile to execute my node command. Don't forget to use the dot before the $HOME var.
By the way, you can run any command attached to your zsh environment.

Related

Crontab and python project

I have a simple python project to water my plants, it works with a Raspberry and RaspberryPi os lite.
In the project I have a package named app with a __main__.py to launch it, I just type python -m app in the terminal and it works fine.
I tried to make a crontab with * * * * * /usr/bin/python /home/pi/djangeau/app
Nothing happens, whereas if I launch a simple python test script it does.
The cron log gives me that error : 'No MTA installed, discarding output' , not sure this is useful to solve the problem.
I hope I was clear enough. Thank you for your answers. Vincent
MTA is for mailing purposes as cron sends email whereas ubuntu doesn't have MTA. Below is the link for a better understanding:
https://askubuntu.com/questions/222512/cron-info-no-mta-installed-discarding-output-error-in-the-syslog
Finally I figured out:
For exemple that command in bash : python -m app
is equivalent to this one for a crontab :
* * * * * cd /home/pi/djangeau && /usr/bin/python -m app
Just replace the correct path and stars by the schedule you want

Not able to use Node.js and Crontab

I know that you can run a Node.js script in Crontab by doing something like:
0 * * * * node /path/to/your/script.js
But I want to run a Node.js app, not a script, using Crontab. I created a Node.js app in order to write some automated tests using Mocha, Chai and Selenium, and I want to run it periodically by using Crontab. How would I go about doing this? I currently run my app by writing in the command line:
npm run api-pro
Where api-pro is a script from my package.json that invokes some tests for the production api.
Note that if I simply try to write a Crontab job with the command "npm run api-pro" it doesn't recognize the command npm (and obviously I do have Node installed in my computer).
My guess is that the user cron use do not configure the PATH in the same way as your user, and do not know node nor npm.
What you can try is to use the command which node to know where your node binary is (/some/path/to/node)
Then you can use the absolute path in your crontab:
0 * * * * /some/path/to/node /path/to/your/script.js
EDIT:
The difference between adding node and npm to $PATH and using absolute paths is that absolute path will work for one executable, since Linux will not have to search the PATH.
Adding to the PATH will make Linux recognize node and npm just as in your user. The fact that they are in the same folder do not affect that.
For anyone trying to run with npm, here were my steps to get it working on MacOS 12.3.1, using node/npm installed by brew.
Note, this is inflexible, and the PATH will need to be updated if brew updates its paths again:
Print two things we will need
echo $PATH
// /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:...
which npm
// /opt/homebrew/bin/npm
Open crontab with EDITOR="vim" crontab -e
Add these two lines
PATH={what you copied from step 1}
* * * * * cd {your_project_dir_where_package.json_lives} && {location_of_npm_from_step_1} run start >>/tmp/crontab.log 2>&1
Note: The >>/tmp/crontab.log 2>&1 is to help you debug. You can tail -f /tmp/crontab.log in another terminal to watch for STDOUT or STDERR
I guess that by using crontab, you're running your node app on a Linux machine so why don't you write a simple bash script ?
run_test.bash
#!/bin/bash
cd /path/to/your/app && \
npm run api-pro
then your crontab should look like :
0 * * * * /path/to/your/bash/script/run_test.bash
Of course, your script will have to be executable for your user :
$ chmod u+x run_test.bash

Pcmanfm set wallpaper fails on Raspbian stretch in cron

I am running a nearly fresh image of Raspbian Stretch 4.9 with a desktop and have a program which creates a new image for the computer background every few minutes.
I am trying to create a cron job to properly update the background using pcmanfm and, having followed the suggestions here, have created the following script called update.sh to set the background:
!#/bin/bash
export DISPLAY=:0
export XAUTHORITY=/home/pi/.Xauthority
pcmanfm -w '/home/pi/folder/image.png'
The script is executable and when it is run from the terminal it functions as intended. I have created a crontab to have this run automatically as such:
* * * * * /home/pi/folder/update.sh > /home/pi/folder/log.txt 2>&1
When the cron job triggers every minute, a pop-up window appears with an error saying "Desktop manager is not active." with a button "OK" to dismiss it, and the log file reads:
** Message: x-terminal-emulator has very limited support, consider choose another terminal
I have tried the command directly in the crontab
* * * * * DISPLAY=:0 && pcmanfm -w '/home/pi/folder/image.png' > /home/pi/folder/log.txt 2>&1
And the error is different this time
Cannot open display:
I am not entirely sure what sense to make of this, though from looking around it seems cron jobs can be finnicky. I am not sure if it is a Path or environment problem because I do not know many details about these things, but I don't think it should be a problem as I am using the full path to the image and the scripts. It shouldn't be a permissions error, because I have tried this on both a user crontab and a system crontab, and both fail. (Besides, the default pi user has root permissions by default anyways.) I am not sure what else to search for or try so I am asking for help if someone could point me in the right direction or has encountered this problem before.
I had exactly the same issue, except I am running Lubuntu 17.04. It appeared lately after a recent update, though cannot pinpoint when. After lot's of research I became suspicious that one of the XDG enviromental variables must be exported too. Following some trial and error, I found that exporting XDG_RUNTIME_DIR solved the problem for me. You may want to give it a try.
To figure out the value run: echo $XDG_RUNTIME_DIR
The working wallpaper changer running from cron for me now looks like:
#!/bin/bash
...
export DISPLAY=:0
export XAUTHORITY=/home/krisz/.Xauthority
export XDG_RUNTIME_DIR=/run/user/1000
pcmanfm --set-wallpaper=${dir}/${file} --wallpaper-mode=crop
...

Install STworkbench on Linux

I am trying to install STworkbench on ubuntu 12.04 but i get an error as below:
************************************************************************
* Looks like you are trying to install on a Ubuntu system *
* *
* This is not officially supported, but can be made to work *
* *
* Look at the following links and carry out the steps listed there *
* and then rerun this script with the --debian flag *
* *
http://stlinux.com/install/ubuntu *
* *
************************************************************************
I followed the installation guide from http://www.stlinux.com/devel/stw.
What can i do to fix this?
Have you tried to add "-d" in your install command:
$ ./install -d stworkbench

cron cannot run composer

I setup composer by following this guide: http://codybonney.com/installing-composer-globally-on-centos-6-4/
it is ok. I could run composer globally.
The problem is that I want to run composer update with cron job.
05 15 * * * cd ~/myproject && composer update --no-dev > ~/composer.tx
error: /bin/sh: composer: command not found
Please help me
Thanks
/bin/sh is probably not the shell you normally use (bash), and it probably has different path settings.
Add the full path to the command so you don't need to change dirs
05 15 * * * /home/user/myproject/composer
...or write your command like ./composer
I solved my problem by using full path to composer installed folder
05 15 * * * cd ~/myproject && /usr/local/bin/composer update --no-dev > ~/composer.tx

Resources