Saturday, 18 May 2013

Vala #8: Useful Functions

1) Get Process ID

public int get_pid_by_name (string name)
{
    try{
        string output = "";
        Process.spawn_command_line_sync("pidof \"" + name + "\"", out output);
        if (output != null){
            string[] arr = output.split ("\n");
            if (arr.length > 0){
                return int.parse (arr[0]);
            }
        }
    } 
    catch (Error e) { 
        log_error (e.message); 
    }

    return -1;
}

This function returns the process ID of the first running instance. You can modify this function to return an array of process IDs for all running instances.

Example:

Pid firefox_id = get_pid_by_name("firefox");


2) Find all child processes invoked by a process

public int[] get_process_children (Pid parentPid)
{
    string output;

    try {
        Process.spawn_command_line_sync("ps --ppid " + parentPid.to_string(), out output);
    }
    catch(Error e){
        log_error (e.message);
    }

    int pid;
    int[] procList = {};
    string[] arr;

    foreach (string line in output.split ("
")){
        arr = line.strip().split (" ");
        if (arr.length < 1) { continue; }

        pid = 0;
        pid = int.parse (arr[0]);

        if (pid != 0){
            procList += pid;
        }
    }
    return procList;
}

This function returns an array of IDs for all processes which were started by the parent process. If you pass the process ID of a running batch script, you will get an array of process IDs for all processes that were started by the batch script. This list can be used to terminate the batch script and the processes invoked by it.


3) Kill a process and its children

public void process_kill(Pid process_pid, bool killChildren = true)
{
    long[] child_pids = get_process_children (process_pid);
    Posix.kill (process_pid, 15);

    if (killChildren){
        Pid childPid;
        foreach (long pid in child_pids){
            childPid = (Pid) pid;
            Posix.kill (childPid, 15);
        }
    }
}

Example:

  • Kill firefox browser:
Pid firefox_id = get_pid_by_name("firefox");
process_kill(firefox_id, true);


4) Freeze and Un-Freeze a process

public int process_pause (Pid procID)
{
    return execute_command_sync ("kill -STOP " + procID.to_string());
}

public int execute_command_sync (string cmd)
{
    try {
        int exitCode;
        Process.spawn_command_line_sync(cmd, null, null, out exitCode);
        return exitCode;
    }
    catch (Error e){
        log_error (e.message);
        return -1;
    }
}

This will freeze the process. Useful for pausing a process that is using a lot of CPU. Please note that you need to un-freeze the process before you can kill it.

public int process_resume (Pid procID)
{
    return execute_command_sync ("kill -CONT " + procID.to_string());
}

This will un-freeze the process and it will continue from the same point.


5) Display notification bubble in system tray

public int notify (string title, string message, int durationMillis, string urgency)
{
    string s = "notify-send -t %d -u %s -i %s \"%s\" \"%s\"".printf(durationMillis, urgency, 
                                                                    Gtk.Stock.INFO, title, message);
    return execute_command_sync (s);
}

This function displays a notification bubble in the system tray for the specified duration. The urgency parameter accepts values "low","normal" and "critical".

Example:

notify("Example","This notification bubble will be displayed for 10 seconds", 10000, "low");

No comments:

Post a Comment

If you are reporting an issue and commenting as an anonymous user, please leave your email address so that I can get in touch with you.