how to run global npm package using crontab? - linux

I have an npm package which is installed globally. If I call it from terminal using global package name from any directory it runs perfectly fine. If I add it in crontab it does not run. It runs from crontab only when I go the directory and call npm start. I want to run it using only a single word command on crontab. Please help.
$ appName //runs fine from any directory
$ cd path/to/npm/package; $ npm start //runs fine
crontab
#reboot appName & #does not work
#reboot npm start -prefix /path/to/npm/package & #works fine

firstly i would advise you to verify whether you are using crontab or cron, since they might behave differently.
secondly, verify the foolowing:
cron job is executed on behalf of the user which successfully able to run the global package. i.e. eliminate user permissions
cron job might need to login to the user shell if you defined some stuff in the user environment to run the global package as you expect

Related

Running a bash script (with some npm commands in it) in Ubuntu by double-clicking on the bash-file runs into `npm: command not found` error

I am currently trying to set up my system so I can run my bash file (startWebApp.bash) on Ubuntu by double-clicking on it. Unfortunately, that is not working, but when I run the script in the terminal with ./startWebApp.bash it works fine.
The underlying problem seems to be that the $PATH variable is different when running the script by double-clicking on the file. The $PATH variable when I run the file in my Terminal is:
/home/magonba/.nvm/versions/node/v14.17.6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
While when I run the file by double-clicking on it, $PATH is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Why is there a difference and how can I change that?
Reproduction steps:
Create the file startWebApp.bash with the following content:
#!/bin/bash
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
I set in the File Manager under Preferences -> Behavior -> Executable Text Files to Ask what to do (in order to be able to start the application by double-clicking on it).
When I double-click on the file, Ubuntu offers me the options: Run in Terminal, Display, Cancel and Run
I choose Run in Terminal (because I need to provide a password since I am using a sudo command)
Runs into the error(s):
/home/my/project/folder/nodejsapp/startWebApp.bash: line 6: npm: command not found
/home/my/project/folder/vuejsapp/startWebApp.bash: line 8: npm: command not found
Thanks for any help in advance!
The problem you got here is, that nvm is integrated in you .bashrc. But when you run your bash file from the GUI, your bashrc will be ignored, and a very minimal bash shell is started. You can fix this in multiple ways.
Install npm to a system installation path (e.g. with apt)
Change your $PATH in the script
#!/bin/bash
PATH="/home/magonba/.nvm/versions/node/v14.17.6/bin:$PATH"
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
Every time you call npm, give the full path
#!/bin/bash
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
/home/magonba/.nvm/versions/node/v14.17.6/bin/npm start &
cd /home/my/project/folder/vuejsapp
/home/magonba/.nvm/versions/node/v14.17.6/bin/npm run serve &
Include your .bashrc in your script
#!/bin/bash
. /home/magonba/.bashrc
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
Please note.
If you go for solution 2 or 3, every time you change your npm version via nvm, you will have to change your script accordingly.

Github actions can not run the config.sh file in the ubuntu 20.04

I am trying to set up the GitHub actions in ubuntu. I made a folder and install the runner using my root account. Now, this is how permissions look like.
When I tried to run
sudo ./config.sh --url https://github.com/user/api --token supersecret
It gives me the error
Must not run with sudo
The solution most people say is that export RUNNER_ALLOW_RUNASROOT="1" and then run the command. But this widely accepted solution is not working for me for some reason.
And some others say create a non-root user and try to run. I tried that way too. It ends up with more errors.
How do I fix this?
I did it this way to run env ❯ sudo env RUNNER_ALLOW_RUNASROOT="1" ./run.sh
for the config just keep the same pattern, put the env at the beginning.

Facing issue with yo jhipster --force shell command

When we are trying to execute yo jhipster --force --skip-git --skip-install from jenkins shell job,
we are receiving the below output:
Easy with the sudo. Yeoman is the master around here.
since yo is a user command, there is no need to execute it with root permissions. if you're having permission errors when using yo without sudo, please spend a few minutes learning more about how your system should work and make any necessary repairs.
Yo is being installed in the server using docker file with the command RUN sh -c 'yo#3.1.1 --unsafe-perm=true --allow-root'
previously we were not facing this issue with the use of yo.
the jenkins shell screenshot

centos7 I set this script to boot, after reboot The service did not start up

Run the shell script with the boot already included
cd /source/finance-calendar
nohup npm run server:prod >/dev/null 2>&1 &
It is possible to start the service normally.
But I set this script to boot, after reboot
The service did not start up, what caused this?
I solve my question.
I add PATH=$PATH:$HOME/bin:/softwate/node-v10.14.2-linux-x64/bin to .bash_profile, so it can run the shell script when i finish starting my system.
But it can't start before user login.
So, I link npm and node to /use/bin,it will start.

npm install - how to run build scripts with sufficient permissions?

I've created a node-module that has a build script that gets called after the installation.
The build script clones a git repository and copies some files of it to another folder.
The problem: on npm install, the script does not get sufficient permissions and I get the following error:
sh: ./build.js: Permission denied
How can I give the build script sufficient permissions to do its job?
I want that the users just can do npm install mymodule and the build-script then does its job on any system.
Any ideas?
Do you have the x flag on build.js?
chmod +x build.js
And the first line of your script should tell how to execute the script from the shell:
#!/usr/bin/env node

Resources