Get process executed by MONO on GNU/Linux - linux

I am using MONO to execute an application. Using ps command shows eihter processname MONO or CLI.
How can I get the name of the application executed by MONO ?
Example : mono myApp.exe
I want to know, if myApp.exe is currently excecuted. Finally I want to do this check programmatically.
Cheers.

You usually will run your program from a shell script and there you can use the -a flag to exec:
#!/bin/bash
exec -a VisibleName mono program.exe

Here is a solution that uses .NET/MONO functions (no need to invoke native DLLs nor piping Shell Output):
List all processes.
If a process Name contains MONO or CLI then read
the commandline of that process
The commandline shall contain all
needed Information to identify your application
public static int process_count(string application_name)
{
int rc = 0;
string cmdline = "";
Process[] processlist = Process.GetProcesses();
foreach (Process p in processlist)
{
cmdline = "";
//Console.WriteLine("PID : " + theprocess.Id + " " + theprocess.ProcessName);
if (p.ProcessName.Contains("mono"))
{
Console.WriteLine("PID : " + p.Id + " " + p.ProcessName + " " + p.MainModule.FileName);
cmdline = File.ReadAllText("/proc/" + p.Id.ToString() + "/cmdline");
Console.WriteLine("CMDLINE : "+cmdline);
}
if (p.ProcessName.Contains("cli"))
{
Console.WriteLine("PID : " + p.Id + " " + p.ProcessName + " " + p.MainModule.FileName);
cmdline = File.ReadAllText("/proc/" + p.Id.ToString() + "/cmdline");
Console.WriteLine("CMDLINE : " + cmdline);
}
if (cmdline.Contains(application_name))
{
Console.WriteLine("Found existing process: {0} ID: {1}", p.ProcessName, p.Id);
rc++;
}
}
return (rc);
}
How to do it manually:
invoke ps -e to get all processes of MONO or CLI
Look up the PID e.g. 2845
Display command line : cat /proc/2845/cmdline
Note for newbies: this Approach is not dedicated to Windows OS as it does not Support the concept of /proc filesystem.
Cheers

Have a look at
getting-mono-process-info
The solution uses the same approach of reading /proc but has more options.
Cheers

Related

Go mail sender inside bash, triggered by cron

I have a Go program that sends mail to me.
package main
import (
"log"
"net/smtp"
"os"
)
func main() {
send(os.Args[2] + " program completed.", os.Args[1], os.Args[2], os.Args[3])
}
func send(body string, to string, s string, date string) {
from := "foo#gmail.com"
pass := "bar"
msg := "From: " + from + "\n" +
"To: " + to + "\n" +
"Subject: "+ s + " main\n\n" +
body + "\n" + date
err := smtp.SendMail("smtp.gmail.com:587",
smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
from, []string{to}, []byte(msg))
if err != nil {
log.Printf("smtp error: %s", err)
return
}
log.Print("sent, visit mail address: "+to)
}
And a bash script that runs it with a mailing list for future preparation,
Do things.....
.
.
filename='list'
while read line; do
# reading each line of list
echo "$(date '+%d-%m-%Y-%T') Mail sent to address : $line" >> ${now}-log.log
./mailsend ${line} foo ${date}
done < $filename
Do final things .....
As you can see that there are simple log attempts to see if the program worked well.
And there are no errors.
When I trigger the program by hand, it works perfectly but from the bash script which is triggered by a cronjob it does not work.
Any suggestions ?
edit1: Variables are solid. Manuel trigger works as intended. When triggered by cron, I do not receive mails.
Maybe you need to cd to your directory before calling ./mailsend?

sshfs with pexpect reported no error but failed to mount (Python 3)

I ask for help!
command = "sshfs " + username + "#" + host + ":" + hostdirectory \
+ " " + mountpoint + " -o nonempty "
sshfs = pexpect.spawn(command)
sshfs.expect(username + "#" + host + "'s password: ")
time.sleep(1)
sshfs.sendline(password)
time.sleep(10)
sshfs.expect(pexpect.EOF)
Runs without error, but /home/user/Mnt/ is empty. I run the code on Linux Mint 20.1.
sshfs should have been killed by SIGHUP prematurely.
Try ignoring SIGHUP like this:
command = "sshfs " + ...
pexpect.spawn('bash', args=['-c', "trap '' HUP; " + command])
...
Same effect for ignoring SIGHUP signal by keyword argument ignore_sighup=True. It's keeping sshfs running in background.
sshfs = pexpect.spawn(command, ignore_sighup=True)
See doc

Qt run shell on terminal

I need to run shell from Qt application in mac
QString strProcess = "/bin/bash ";
strProcess += (QDir::currentPath() + "/../../../apk_build.sh");
strProcess += " -a " + ui->textEdit_apk->toPlainText();
strProcess += " -o " + ui->textEdit_out->toPlainText();
strProcess += " -c " + ui->textEdit_channel->toPlainText();
QProcess process;
process.execute(strProcess);
here some problem.
problem 1: it can not show content info in terminal , I need to see running info.
problem 2: it can not find apktool: command not found. apktool can be find if I execute command in terminal without Qt Application(apktool path: /usr/bin/apktool).
Problem1
If you want just to save output of the process then just set standard output of process.
void QProcess::setStandardOutputFile ( const QString & fileName, OpenMode mode = Truncate )
If you want to get output in real time then you have to handle your process as standard sequential I/O device by calling read (), readLine () functions.
Problem 2
You have to load environment variable of you user. Try to:
source /etc/profile

Can't manipulate the string in powershell while calling the script in C# Windows Application

I am trying to take backup of SQL Server database using PowerShell and then making windows application around it. But it is not working. I think I have mingled up the strings by putting wrong double quotes and single quotes. Following is the code in C# where I am calling the PowerShell script.
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Import-Module SQLPS -DisableNameChecking");
pipeline.Commands.AddScript("$dt=Get-Date -Format '" + dtFormat + "' ");
pipeline.Commands.AddScript("$dbname = '" + dbName + "'");
pipeline.Commands.AddScript("$backup = " +bckFolderPath +#"\$($dbname)_db_$($dt).bak");
pipeline.Commands.AddScript("Backup-SqlDatabase -ServerInstance '" + serverName + "' -Database $dbname -BackupFile" + " $backup");
runspace.Close();
What can I do as this is not taking backup but if I run the script in PowerShell, then it is working.
You do realize that the commands you provided in your comment (that you say are working in PS) aren't the same as the ones you push through c#? The $backup line doesn't exist in your PS commands...
Anyway, I believe the error could be in the $backup line, missing quotes around the path...
pipeline.Commands.AddScript("$backup = '" +bckFolderPath +#"\$($dbname)_db_$($dt).bak'");

Perl program using wrong arguments

I am trying to create simple startup script in perl that will launch various programs on system startup. It is as follows
my #startupPrograms = qw(google-chrome thunderbird skype pidgin );
my #pagesToBeOpenedInChrome = qw(http://www.google.com/ http://stackoverflow.com/ https://mail.google.com/mail/u/0/#inbox);
sub runPrograms() {
print("Starting startup Programs... \n");
foreach (#startupPrograms) {
my $command = $_;
print "Starting Program " . $command . "\n";
if($command == "google-chrome") {
foreach (#pagesToBeOpenedInChrome) {
$command = $command . " " . $_;
}
}
`$command &`;
print "Program " . $command . " started \n";
}
}
But the output I am getting is
[aniket#localhost TestCodes]$ ./startUp.pl
***** Welcome to startup program! *****
Starting startup Programs...
Starting Program google-chrome
Program google-chrome http://www.google.com/ http://stackoverflow.com/ https://mail.google.com/mail/u/0/#inbox started
Starting Program thunderbird
Program thunderbird http://www.google.com/ http://stackoverflow.com/ https://mail.google.com/mail/u/0/#inbox started
Starting Program skype
Program skype http://www.google.com/ http://stackoverflow.com/ https://mail.google.com/mail/u/0/#inbox started
Starting Program pidgin
Program pidgin http://www.google.com/ http://stackoverflow.com/ https://mail.google.com/mail/u/0/#inbox started
Why is taking the pagesToBeOpenedInChrome array contents in each command to be executed? Also it looks like if i put pidgin before other programs it takes forever to start pidgin(other programs are locked). Any help or suggestion is appreciated.
change
if($command == "google-chrome") {
to
if($command eq "google-chrome") {
in perl, you use eq or ne for text comparisons and == for numeric comparisons!
use the == for text basically says if $command is not empty or 0

Resources