Tasks runs only after admin is logged in
Got it. Added the following code.

if (!craft()-&gt;tasks-&gt;isTaskRunning()){
    $task = craft()-&gt;tasks-&gt;getNextPendingTask();

    if ($task){
        craft()-&gt;tasks-&gt;runPendingTasks();
    }
}