I want to use syslog-ng to receive netgear log, and use python script process.
But syslog-ng didn't run the python script.
syslog-ng.config
#version:3.2
options {
flush_lines (0);
time_reopen (10);
log_fifo_size (1000);
long_hostnames (off);
use_dns (no);
use_fqdn (no);
create_dirs (no);
keep_hostname (yes);
};
source s_sys {
udp(ip(0.0.0.0) port(514));
};
destination d_python{
program("/usr/local/bin/python /opt/syslog.py");
#program("/bin/echo 'haha' >> /tmp/test");
};
log { source(s_sys); destination(d_python);};
and python script like this
#!/usr/local/bin/python
#coding:utf8
import os
import sys
import datetime
f = open('/var/log/pnet.log', 'a')
f.write('\nstart\n')
f.write('args\n')
f.write('%s\n' % sys.argv)
if not sys.stdin.isatty():
f.write('stdin\n')
f.write('%s\n' % date.date.now().isoformat() )
tty_read = sys.stdin.read()
f.write("'''\n")
f.write(tty_read)
f.write("\n'''\n")
f.write('end\n')
f.close()
The script is already 777
Even I change my config to use 'echo' directly pipe into a file, didn't write a word too...
So...why?
silly question, but do you have incoming logs? If you use a simple file destination instead of the program, do you receive logs? If not, the problem is not in the program destination.
Also, try changing the flush_lines (0); option to 1 to see if it helps.
Regards,
Robert Fekete
I could show you my code for reference:
my syslog-ng.conf :
source s_test{
file("/home/test/in.log" follow-freq(1) flags(no-parse));
};
destination d_test{
program ("/home/test/out.py" flush_lines(1) flags(no_multi_line));
};
log {
source(s_test);
destination(d_test);
flags(flow-control);
};
my out.py:
#!/usr/bin/env python
# encoding: utf-8
import sys
while True:
line = sys.stdin.readline()
file = open("/home/test/out_from_python.log","a")
file.write(line)
file.close()
when you type echo "something" >> /home/test/in.log , there would be a new log lie in /home/test/out_from_python.log
Related
I'm facing quite simple problem, yet still not able to figure out what in particular causes that, and - more importantly - how to solve it.
I'm currently on Linux, and I would like to run an python app (script) from a Java application. The script is on PATH, thus I really would like to utilise that (avoid using absolute path to the script), if possible.
However, all I tried resulted in various forms of "File does not exist".
Sample
As a demonstration, I've tried to run one python3-based app (meld)
and one binary-built app (ls) for comparison:
$ which ls
/usr/bin/ls
$ file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=[...], for GNU/Linux 3.2.0, stripped
$ which meld
/usr/bin/meld
$ file /usr/bin/meld
/usr/bin/meld: Python script, UTF-8 Unicode text executable
$ head -n1 /usr/bin/meld
#!/usr/bin/python3
Nextly, I've created simple Java main, which tries several ways how to start theese:
package cz.martlin.processes;
import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.util.List;
import java.util.stream.Collectors;
public class ProcessPlaying {
public static void main(String[] args) {
List<List<String>> commands = List.of(
List.of("ls"),
List.of("/usr/bin/ls"),
List.of("meld"),
List.of("/usr/bin/meld"),
List.of("python3", "/usr/bin/meld"),
List.of("/usr/bin/python3", "/usr/bin/meld"),
List.of("sh", "-c", "meld"),
List.of("sh", "-c", "/usr/bin/meld"),
List.of("sh", "-c", "python3 /usr/bin/meld")
);
for (List<String> command : commands) {
run(command);
}
}
private static void run(List<String> command) {
System.out.println("Running: " + command);
//String executable = command.get(0);
//boolean exists = new File(executable).exists();
//System.out.println("Exists the " + executable + " ? " + exists);
try {
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectError(Redirect.INHERIT);
Process proc = pb.start();
// Process proc = Runtime.getRuntime().exec(command.stream().collect(Collectors.joining(" ")));
int code = proc.waitFor();
System.out.println("OK, return code: " + code);
} catch (IOException e) {
System.out.println("Failed to start: " + e.toString());
} catch (InterruptedException e) {
System.out.println("Failed to await: " + e.toString());
}
System.out.println();
}
}
Here are the results:
Running: [ls]
OK, return code: 0
Running: [/usr/bin/ls]
OK, return code: 0
Running: [meld]
Failed to start: java.io.IOException: Cannot run program "meld": error=2, Directory or file doesn't exist
Running: [/usr/bin/meld]
Failed to start: java.io.IOException: Cannot run program "/usr/bin/meld": error=2, Directory or file doesn't exist
Running: [python3, /usr/bin/meld]
python3: can't open file '/usr/bin/meld': [Errno 2] No such file or directory
OK, return code: 2
Running: [/usr/bin/python3, /usr/bin/meld]
/usr/bin/python3: can't open file '/usr/bin/meld': [Errno 2] No such file or directory
OK, return code: 2
Running: [sh, -c, meld]
sh: line 1: meld: command not found
OK, return code: 127
Running: [sh, -c, /usr/bin/meld]
sh: line 1: /usr/bin/meld: Directory or file doesn't exist
OK, return code: 127
Running: [sh, -c, python3 /usr/bin/meld]
python3: can't open file '/usr/bin/meld': [Errno 2] No such file or directory
OK, return code: 2
To sum it up:
all of the commands tried works when executed directly from the shell (either sh or bash)
executing the binary works either by the full path (/usr/bin/ls) or by just the name (ls)
executing the python script the same way doesn't work neither way
when trying to run the python3 interpreter and populate the script path as an argument to the python, now the python yields the script file doesn't exist
trying to populate it as a command to the brand new shell didn't help either
I've tried to use the Runtime#exec (based on this comment: https://stackoverflow.com/a/36783743/3797793) to start the process (both the exec(String) and exec(String[] forms), but no sucess (none of the listed commands did actually execute).
Thus, my question is/are:
What do I understand wrong?
How to start the Python script from Java?
Would some small (ba)sh script wrapper do the job?
Since it's python3-based, Jython wouldn't help here (because the latest one is 2.7.*), would it?
Further requirements:
As mentioned, I would like to be able to avoid using full path to the Python script
Also, I would like to have platform independant solution (at least Linux and Windows compatible)
Thanks in advance
So I'm trying to run a simple python file called test.py through the Node JS child process, however it keeps saying that python3: can't open file './test': [Errno 2] No such file or directory. I tried everything to fix this error. For example, the python script ran successfully from the same directory the typescript file is present, I even tried the full path, the relative path for the python file and it still didn't work. I kindly request someone too look into this, thank you!
test.py
import sys
print("hello world")
sys.stdout.flush()
var spawn = require("child_process").spawn;
var child = await spawn("python3", ['./test.py']);
let dataMain = "";
child.stdout.on("data", (data: Buffer) => {
// Do something with the data returned from python script
console.log(data); // buffer data
dataMain += data.toString();
});
child.stdout.on("end", () => {
console.log("end");
console.log(dataMain);
});
child.stderr.pipe(process.stderr); // prints out the error
Worked with ${process.cwd()}/test.py
My python script has two main steps:
Open two webbrowser tabs (default browser). And let a LED blink.
When i am executing the python script via shell with "python netflix.py" everything works fine.
But when i am trying to start it via my (see below) NodeJS script. Only the LED will be blink. The webbrowser tabs won't come up.
Does anyone know where the issue is?
#!/usr/bin/env node
process.env.UV_THREADPOOL_SIZE = 1208;
var http = require('http');
const server = http.createServer(function(request, response) {
try{
console.log('Request incoming...');
var spawn = require('child_process').spawn;
if(request.url == '/abort'){
console.log('Calling abort.py ...');
var process = spawn('python',["./abort.py"]);
}else{
console.log('Calling netflix.py');
var process = spawn('python',["./netflix.py"]);
}
}catch(e){
console.log('Error');
console.log(e);
}
});
server.on('error', function(){
console.log('error');
});
const port = 8000;
server.listen(port);
server.timeout = 10000;
console.log(`Listening at http://leitner-pi:${port}`);
import sys
import webbrowser
from gpiozero import LED
from time import sleep
try:
webbrowser.open_new_tab("http://netflix.com");
finally:
print("")
print("Lasse PIN 7 blinken..")
led = LED(17)
while True:
led.on()
sleep(0.3)
led.off()
sleep(0.3)
Fixed: Well, I made a workaround.
First, I split the python script into two scripts. Webbrowser and LED script, to avoid any kind of interruptions or something else. Second, I changed the webbrowser to:
os.system(‘sudo -upi chromium-browser URL1 URL2’).
At last, I am calling both new
scripts from my node webserver, like I used before.
Now it works fine.
I'm running python in a node web app, and I'm trying to load and read a file in python, do something with it, then spit it out to node.js.
When I run the following python code, nothing happens.
Python
import json
import sys
with open('trainingData.json') as file:
data = json.load(file)
print(data)
print('hello from python')
sys.stdout.flush()
When I remove with open, then it works well. How can I read a file in python and call that file in node.js? Here's the node code
Node
app.get('/', (req, res) => {
const spawn = require('child_process').spawn;
const process = spawn('python', ['./python/script.py', 'Hello', 'World']);
process.stdout.on('data', data => console.log(data.toString()));
res.send('he');
});
(When I run the python file fro the terminal, it works correctly.)
You can use spawn's cwd (current working directory) option, to specify the directory. To set it to the "current" current working directory use __dirname.
const process = spawn('python', ['./python/script.py', 'Hello', 'World'], {cwd: __dirname});
I'm trying to write a vim plugin that uses a Python code block inside of it. I would like to get the full path of myvim.vim (/home/myusername/.vim/bundle/myvim/plugin/myvim.vim) inside of my python code block. Unfortunately you can't get the path by using __file__ as in a .py file. I can't use vim.command(':pwd') either because that just prints the path of the location where the plugin function is called from.
myvim.vim
function! Myvim()
python << EOF
import vim
vim_path = "full myvim.vim path here"
print vim_path
EOF
endfunction
EDIT
#actionshrimp, I'm trying this:
myvim.vim
function! Myvim()
let s:curfile = expand("<sfile>")
let s:curfiledir = fnamemodify(s:curfile, ":h")
python << EOF
import vim
py vim_path = vim.eval('expand("<sfile>")')
print vim_path
EOF
endfunction
You can use <sfile> to get the path of the currently executing vimscript, like so:
let s:curfile = expand("<sfile>")
let s:curfiledir = fnamemodify(s:curfile, ":h")
To pass that to python you should be able to use:
py vim_path = vim.eval('expand("<sfile>")')
or if you've set the variable:
py vim_path = vim.eval('s:curfile')
For clarity here's a full example (saved as 'D:\tmp\test.vim'):
python << EOF
import vim
vim_path = vim.eval('expand("<sfile>")')
print vim_path
EOF
When I have it open and type :so % it shows 'D:\tmp\test.vim' at the bottom.