Calling python script from bash stops at command prompt - python

I am calling a python script:
/bin/sh
python ~/Documents/Projects/Programming/Python/svg/svg2dxf.py $1 0
After running the script, I get a python command prompt ($) and it's only when I type "exit" at the command prompt that the script runs.
What am I doing wrong?

Change the following line:
/bin/sh
with (shebang interpreter directive):
#!/bin/sh
Otherwise, the new shell instance is invoked; until the new shell is exit, next line is not executed.

You should remove the leading:
/bin/sh
Your current script does two things:
1) execute a new instance of /bin/sh
==> which gives your the shell $ sign
2) execute the python script
Your script should be:
python ~/Documents/Projects/Programming/Python/svg/svg2dxf.py $1 0

Related

Not getting prompt after executing python script from bash

I am executing a python script from bash script as follows
"python -O myscript.pyo &"
After launching the python script I need to press "enter" manually to get prompt.
Is there a way to avoid this manual intervention.
Thanks in advance!
pipe a blank input to it:
echo "" | python -O myscript.pyo
you might want to create a bash alias to save keyhits: alias run_myscript="echo '' | python -O myscript.pyo"
placing wait after the line to run the process in background seems to work.
Source:
http://www.iitk.ac.in/LDP/LDP/abs/html/abs-guide.html#WAITHANG
Example given:
!/bin/bash
test.sh
ls -l &
echo "Done."
wait
Many thanks

Return prompt after accessing nohup.out (bash)

I'm running a Python script from bash using nohup. The script is executed via my bashrc as part of a shell function. If I run it like this:
function timer {
nohup python path/timer.py $1 $2 > path/nohup.out 2>&1 &
echo 'blah'
}
Everything works and I get my prompt back. However, if instead of echo I call tail to access the end of the nohup output file, like this:
function timer {
nohup python path/timer.py $1 $2 > path/nohup.out 2>&1 &
tail -f path/nohup.out
}
my prompt is not returned. I would like to see the contents of nohup.out and get back to the prompt without having to use CTRL-c.
I have followed the advice here, but adding </dev/null yields the same results as above.
You won't get prompt.
Because, tail -f will always watch the file (path/nohup.out) to output appended data as the file grows. You can try tail -n to get last 10 lines of path/nohup.out.

Run python script from rc.local does not execute

I want to run a python script on boot of ubuntu 14.04LTS.
My rc.local file is as follows:
sudo /home/hduser/morey/zookeeper-3.3.6/bin/zkServer.sh start
echo "test" > /home/hduser/test3
sudo /home/hduser/morey/kafka/bin/kafka-server-start.sh /home/hduser/morey/kafka/config/server.properties &
echo "test" > /home/hduser/test1
/usr/bin/python /home/hduser/morey/kafka/automate.py &
echo "test" > /home/hduser/test2
exit 0
everything except my python script is working fine even the echo statement after running the python script, but the python script doesnt seem to run.
My python script is as follows
import sys
from subprocess import Popen, PIPE, STDOUT
cmd = ["sudo", "./sbt", "project java-examples", "run"]
proc = Popen(cmd, shell=False, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
proc.communicate(input='1\n')
proc.stdin.close()
which works perfectly fine if executed individually.
I went through the following questions , link
I did a lot of research but couldn't find a solution
Edit : echo statements are for testing purpose only, and the second actual command (not considering the echo statements) is starting a server which keeps on running, and even the python script starts a listener which runs on an infinite loop, if this is any help
The Python script tries to launch ./sbt. Are you sure of what if the current directory when rc.local runs? The rule is always use absolute paths in system scripts
Do not run the Python script in background, run it in foreground. Do not exit from its parent script. Better call another script from "rc.local" that does all the job of "echo" and script launching.
Run that script from "rc.local"; not in background (no &).
You do not need "sudo" as "rc.local" is run as root.
If you want to run python script at system boot there is an alternate solution which i have used.
1:Create sh file like sample.sh and copy paste following content
#!/bin/bash
clear
python yourscript.py
2:Now add a cron job at reboot.If you are using linux you can use as following
a:Run crontab -e(Install sudo apt-get install cron)
b:#reboot /full path to sh file > /home/path/error.log 2>&1
And restart your device

subprocess Popen in python with command that changes environment

I'm trying to run a python script from python using the subprocess module and executing a script sequentially.
I'm trying to do this in UNIX but before I launch python in a new shell I need to execute a command (ppack_gnu) that sets the environment for python (and prints some lines in the console).
The thing is that when I run this command from python subprocess the process hangs and waits for this command to finish whereas when I do it in the UNIX console it jumps to the next line automatically.
Examples below:
From UNIX:
[user1#1:~]$ ppack_gnu; echo 1
You appear to be in prefix already (SHELL=/opt/soft/cdtng/tools/ppack_gnu/3.2/bin/bash)
1
[user1#1:~]$
From PYTHON:
processes.append(Popen("ppack_gnu; echo 1", shell=True, stdin = subprocess.PIPE))
This will print Entering Gentoo Prefix /opt/soft/cdtng/tools/ppack_gnu/3.2 - run 'bash -l' to source full bash profiles
in the python console and then hang...
Popen() does not hang: it returns immediately while ppack_gnu may be still running in the background.
The fact that you see the shell prompt does not mean that the command has returned:
⟫ echo $$
9302 # current shell
⟫ bash
⟫ echo $$
12131 # child shell
⟫ exit
⟫ echo $$
9302 # current shell
($$ -- PID of the current shell)
Even in bash, you can't change environment variables of the parent shell (without gdb or similar hacks) that is why source command exists.
stdin=PIPE suggests that you want to pass commands to the shell started by ppack_gnu. Perhaps you need to add process.stdin.flush() after the corresponding process.stdin.write(b'command\n').

Python paramiko hangs when trying to run bash shell

I need to run a regression script in bash shell on a remote server. I am able to successfully connect and execute different commands using paramiko. But when I try to execute '/bin/bash' my Python script hangs forever:
stdin,stdout,stderr = ssh.exec_command("pwd;/bin/bash;echo $SHELL")
Without /bin/bash echo $SHELL works good and returns the following:
[u'/home/akar\n', u'/tools/cfr/bin/tcsh\n']
Is there any workaround?
My first doubt is what is the purpose of the bash you are executing. Literally it means:
pwd; #print '/home/akar\n' as it results
/bin/bash; #/bin/bash will take over of console(stdin/stdout/stderr) from here
echo $SHELL #when you input exit to exit the bash from line 2, line 3 will print

Categories