Can someone tell me how I can do "npm run start" in any folder using a Python script. But please with the "os" operator and not "subprocess".
EDIT:
I need a python script, that goes to a specific folder and then executes
"npm run start". How can I do that?
You can run code in selecte folder
os.chdir("path/to/folder")
os.system("npm run start")
or
os.system("cd path/to/folder ; npm run start")
os.system("cd path/to/folder && npm run start")
or
subprocess.run("npm run start", shell=True, cwd="path/to/folder")
subprocess.run(["npm", "run", "start"], cwd="path/to/folder")
and similar way with other methods in subprocess
Related
I have to run a docker and then a command inside the workdir using a python script.
i'm triyng to do it as follows:
command = ['gnome-terminal', '-e', "bash -c 'sudo /home/mpark/Escriptori/SRTConverter/shell_docker.sh; echo b; exec $SHELL'"]
p = subprocess.Popen(command)
where 'sudo /home/mpark/Escriptori/SRTConverter/shell_docker.sh' is a shell script with the docker run with root privileges
the fisrt command 'sudo /home/mpark/Escriptori/SRTConverter/shell_docker.sh' works good, but the second one 'echo b' that has to run inside the container doesn't work..
Thank you!
Im trying to run npm start and flask run at the same time in terminal on Mac, I tried to do it with shell script -> .sh like this
./chinmap_flask.sh
cd INS/FYP/chinmap
export PORT=3001
npm start
==================================
chinmap_flask.sh
#!/bin/sh
cd ~
cd Desktop
cd INS/FYP/chinmap/src/Backend/frontendData
export FLASK_APP=../app
export FLASK_ENV=development
flask run
==================================
And this is not working too:
"scripts": {
"start": "react-scripts start && cd ~ && cd Desktop && ./chinmap_flask.sh"
},
npm server started successfully, however, the terminal stop running other scripts after npm start, are there any possible ways to run both of them in one shell script or single line of terminal command? Thank you!
I don't think it is possible to run two commands at the same time. You can try this but I'm not sure if it will work:
npm start && flask run
You could try adding a new tab in the terminal with Cmd + T And in the first tab run flask run and run npm start in the second tab. This won't run both the commands in a single terminal command but the node server and the flask server will be running.
If you have trouble doing this check out this post: https://superuser.com/questions/286157/how-can-i-open-a-new-tab-in-terminal-in-mac
Just add & at the end of any command to run it in the background and get your Terminal back for doing other stuff:
# Start flask in background
./chinmap_flask.sh &
# Set up and start npm in background
cd INS/FYP/chinmap
export PORT=3001
npm start &
Using ubuntu's 16.04 crontab and #reboot to run python3 script. The script runs properly on reboot as I see the logged output. However, my script's os.system command is not running. It runs fine if ran outside of crontab. My scripts are all executable.
crontab -l output:
SHELL=/bin/bash
#reboot nohup /usr/bin/python3 -u /home/path/scheduler.py >> /path/log.out &
scheduler.py code:
#...(check if web server is running...if not restart)
os.system('nohup /usr/bin/python3 -u /path/webserver/main.py &')
print('this function ran')
When I logged the output of the os.system command , there was no output.
As a side note, I am running python schedule commands to check the general health of a webserver. crontab doesn't seem to be the right tool for this so I just use crontab to start my python scheduler on reboot.
I am using flask as the webserver, and would use gunicorn and systemctrl if I could get it to work... but it didn't so this is my workaround.
The point is that, the command called by os.system is not in default path.
For example, tcpdump is not in /usr/bin/.
So, you can solve the problem by adding the full path of the command.
I was facing the same issue when we try to run python script directly in crontab it just by passes the os.system() commands.
Make launcher.sh:
#!bin/bash
cd /home/pi/
sudo python example.py
Then, make your script executable:
chmod 755 launcher.sh
And at last, add your script to crontab:
crontab -e
and add this line at the end:
#reboot sh /home/pi/launcher.sh
(I set the program to run at each reboot)
I have a simple python script which I want to start a daemon-service in background in docker container
/sbin/start-stop-daemon --start --user root --make-pidfile --pidfile /var/lock/subsys/my-application.pid --exec 'python /opt/app/uc/monitor/bin/my-application.py'
when I execute this command in a shell I get
/sbin/start-stop-daemon: unable to stat //python /opt/app/uc/monitor/bin/my-application.py (No such file or directory)
However when execute just the below command in shell it works
python /opt/app/uc/monitor/bin/my-application.py
I'm sure the python is installed and all the links have been setup.
Thanks for the help
That error message implies that start-stop-daemon is looking for a file to open (the stat operation is a check before it opens the file) and treating your 'python ... ' argument as if it was a file.
See this example which confirms this. You may need to read the man page for start-stop-daemon, for your Ubuntu version, to check what a valid command would be for your setup.
Simplest solution is probably to create a shell script (say /opt/app/uc/monitor/bin/run-my-application.sh), and put this into it:
#!/bin/bash
python /opt/app/uc/monitor/bin/my-application.py
Be sure to do chmod +x on this file. If python is not found, use which python to find the path to python and use that in the script.
Now try:
/sbin/start-stop-daemon --start --user root --make-pidfile --pidfile /var/lock/subsys/my-application.pid --exec '/opt/app/uc/monitor/bin/run-my-application.sh'
Tried googling but couldn't find something that relates to my particular problem. I'm trying to run a shell script from python but the shell script wouldn't run because of a permission denied error. The python code I'm running is:
process = subprocess.Popen('run.sh', shell=True, stdout=subprocess.PIPE)
process.wait()
....
os.killpg(pro.pid, signal.SIGTERM)
The error I'm getting:
python RunScript.py "input"
/bin/sh: 1: run.sh: Permission denied
The contents of my shell script is:
#!/bin/sh
abspath=$(cd "$(dirname "$0")"; pwd)
CLASSPATH=$CLASSPATH:$abspath/"lib/*":$abspath/"bin"
export CLASSPATH
java -classpath $CLASSPATH my.folder.path.Class $abspath/../data/data.txt $abspath/../data/data2.txt
Thanks in advance.
Check your run.sh mode, if no executable flag, set it with command
chmod +x run.sh
its because you don't have permission to run that script. You will need to give executable permission for that script to run.
chmod a+x run.sh