I am working with Ubuntu 16.04 on an NVIDIA TX2 system and have a shell script launcher.sh containing the following commands:
#!/bin/sh
cd /home/nvidia
sudo python test.py >> /home/nvidia/test_output.txt,
basically implying to store the output of test.py in test_output.txt.
The test.pyhas one line: print "Testing startup script!"
I want this script (and more complicated scripts later on) to run automatically when the system boots up. To this end, I added it to the Startup Applications with the command /home/nvidia/launcher.sh and rebooted the system. The file test_output.txt is indeed created but there is no output written in it. I tested the script from the terminal and it works fine.
How do I make this to work from Startup Applications?
Init scripts often don't have the complete environment initialized, e. g. PATH. In order to be executed you better provide the complete paths to commands.
On the other hand sudo is unnecessary here, as you obviously don't do anything that needs root permissions. If you need it, be aware that sudo is asking interactively for a password, which stalls the execution of the script, if the command you try to execute with it isn't explictely permitted in /etc/sudoers.
Provided test.py is located in /home/nvidia, and python in /usr/bin, the script should read
#!/bin/sh
cd /home/nvidia
/usr/bin/python test.py >> test_output.txt
And if you want to add error output to the outfile, which may be useful for debugging, make the last line
/usr/bin/python test.py >> test_output.txt 2&>1
or for a separate error log
/usr/bin/python test.py >> test_output.txt 2> test_error.log
Related
I've got a batch script that is pretty straight forward:
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login -i -c "cd C:\python-script && python run.py"
I've not done a lot with batch scripts before, but here I can tell it's just opening up git bash interactively, then running the command to CD in a directory and run python run.py
When I run this .bat from a cmd prompt, git bash opens up ever so briefly and then immediately closes but the script is never fired off.
Any way I can stop that script temporarily so I can see any error the git window may be throwing?
In order to allow the pause to display in the current window, we can test by removing the start command to execute it in the local window. Also as mentioned in comments, if python is installed and it does not find it, it would require you to specify full path to it which should then work:
"%ProgramFiles(x86)%\Git\bin\sh.exe" --login -i -c "cd C:\python-script" "C:\python37\python.exe" run.py
I have a Python program, A.py, that creates binary data upon completion. To help users analyze the output, I want to add a small script, B.sh, to the output directory that fires up a Python console and executes some commands, C, that load the data and prepare them such that a user sees what is available. After executing C, the script B.sh should keep the Python console open.
First attempt at B.sh:
I figured out that
#!/bin/sh
xterm -e python
opens a Python console and keeps it open but doesn't execute anything within that console.
Second attempt at B.sh:
I figured out that
#!/bin/sh
xterm -e python -i C.py
executes C.py (I'd prefer not to have to write an additional file for the startup commands, but I could live with that) and keeps the window open, but doesn't show what was done. More specifically, the user would be presented with the outputs of C, but not the command that were used to achieve the outputs.
Instead,I'd like the user to be presented with a console like this:
>>> [info,results] = my_package.load(<tag>)
>>> my_package.plot(results)
>>> print(info)
<output>
>>> my_package.analyze(results)
<output>
>>>
Save this in a file called demo.tcl
#!/usr/local/bin/expect -f
# Spawn Python and await prompt
spawn /usr/local/bin/python3
expect ">>>"
# Send Python statement and await prompt
send "print('Hello world!')\n"
expect ">>>"
# Pass control to user so he can interact with Python
interact
Then make it executable with:
chmod +x demo.tcl
And run with:
xterm -e ./demo.tcl
In the picture, you can see I went on after the "Hello world" to print the system version info.
Your paths for Python and expect may be different, so check and alter to suit.
For anyone who happens to be using macOS (a.k.a. OSX), you can install expect with homebrew as follows:
brew install expect
And, since Macs don't ship with X11 any more, rather than install XQuartz and run xterm, you can start a new Terminal and run the Python in there quite simply with:
open -a Terminal.app demo.tcl
As suggested by user #pask I simply printed the commands before executing them.
Furthermore, I added a -c switch to be able to put the Python commands directly in B.sh instead of having to write a file C.py
Here is the B.sh I am using now:
#!/bin/sh
xterm -e python -i -c "print('>>> import my_package');import my_package;print('>>> [info,results] = my_package.load()');[info,results] = my_package.load()"
I'm trying to make a Python script run as a service.
It need to work and run automatically after a reboot.
I have tried to copy it inside the init.d folder, But without any luck.
Can anyone help?(if it demands a cronjob, i haven't configured one before, so i would be glad if you could write how to do it)
(Running Centos)
run this command
crontab -e
and then add
#reboot /usr/bin/python /path/to/yourpythonscript
save and quit,then your python script will automatically run after you reboot
There is no intrinsic reason why Python should be different from any other scripting language here.
Here is someone else using python in init.d: blog.scphillips.com/posts/2013/07/… In fact, that deals with a lot that I don't deal with here, so I recommend just following that post.
For Ubuntu Variant:-
open the /etc/rc.local file with:
nano /etc/rc.local
add the following line just before the exit 0 line:
start-stop-daemon -b -S -x python /root/python/test.py
or
Give absolute path of your command i.e
nohup /usr/bin/python2 /home/kamran/auto_run_py_script_1.py &
The start-stop-daemon command creates a daemon to handle the execution of our program. The -b switch causes the program to be executed in the background. The -S switch tells the daemon to start our program. And the -x switch tells the daemon that our program is an executable.
To check and Run
sudo sh /etc/rc.local
This is probably a very simple question, but I just can't figure it out. I've coded a Python GUI application (uses PyQT), and all I want to do is to launch it with an executable script. I'm still debugging the application, so it'd be nice if the terminal stays open to see any errors/print statements/exceptions thrown.
This is what I've got in my script:
#!/bin/sh
x-terminal-emulator -e cd dirname && python GUIapp.py
It successfully runs the Python application, but once the application loads, the terminal automatically closes. The application continues to run after the terminal closes.
I know I can open a terminal and then simply type in "cd dirname && python GUIapp.py", but I'm lazy.
What am I missing here?
Use --hold or --noclose option. They have the same function with different name.
So change script to
#!/bin/sh
x-terminal-emulator --noclose -e cd dirname && python GUIapp.py
If you are looking for a command option, always check --help. In this case it gives you the needed information.
user#host:~/projects$ x-terminal-emulator --help
Usage: x-terminal-emulator [Qt-options] [KDE-options] [options] [args]
Terminal emulator
Generic options:
[...]
Options:
[...]
--hold, --noclose Do not close the initial session automatically when it ends.
[...]
I want to run a python script at boot of Lubuntu 15.04. This python script writes some string into a text file placed in /home/aUser/aFile.txt
My /etc/rc.local file is:
#!/bin/sh -e
python /home/aUser/theScript.py &
exit 0
And the script /home/aUser/theScript.py is:
#!/usr/bin/python
f = open('/home/aUser/aFile.txt','w');
f.write("Some string...");
f.close();
Actually the python script does more, and run an infinite loop, this is why I run the script in background with &. Of course I have python installed:
~$ python --version
Python 2.7.9
I checked if /etc/rc.local is called at boot, and it is, proof of that: I added a test into the /etc/rc.local in this way:
#!/bin/sh -e
python /home/aUser/theScript.py &
exit 0
echo "Test" >> /home/aUser/aTest.txt
and the file /home/aUser/aTest.txt is written/created at boot.
So everything looks correct, proof of that:
if I run manually ~$ /etc/rc.local the file aFile.txt is correctly written.
Instead if I start (or reboot) the OS, the file is not written at boot.
I suspect that could be a problem of permissions/user: I know that /etc/rc.local is run as root, but even if I set root or aUser as owner of the file, the situation is the same. Also run the python script in the /etc/rc.local as user aUser (with su command) does not solve the problem.
Ok I found the problem and fix it, thanks to the #Zac comment.
Actually the python script try to open a network connection before writing the file: at boot time, when the python script is run from /etc/rc.local (so, it is run), the network is still not ready (probably because it is a wireless network) and therefore an exception is raised and the entire script stops. Capturing the exception solves the problem.
So at the end it was my fault, (not) helped by the rc.local that does not provide an easy way to debug.