I am trying to automate PBS script generation and qsub submission.I have N folders each containing sub folders. I would like to move to each folder and subfolder, create a PBS script and submit the PBS script from that folder. I wrote a perl script to do the same which runs fine. The PBS script is created without any issues, but when submitted using qsub, the job doesn't get executed.
Interestingly, I observe that the job gets executed if I open the PBS script in a text editor and save it ( using ":wq" in vim, does not work if I quit without saving ":q" )
I am wondering why this happens, any solutions to automate ? I understand that there are ways to do this without changing folders and sub folders - by directly providing file paths, but I am trying to understand whats going wrong here.
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy 'move';
use Cwd;

################################################## ################

my @folders = ("folder1","folder2","folder3");
my $original_path = cwd();
my $queue_name = $ARGV[0];

################################################## ################

# Call function for each folder in the list

foreach (@folders) {
submit_qsub($_);
}

################################################## ################

# Function to change folders, call another function to make PBS script, submit PBS script using system command

sub submit_qsub {
my $folder = $_;
print "\nFunction for $folder called\n";
chdir ("./$folder" or die "Can't cd to $folder $!\n");
chdir ("./V1");
mkdir ("jobname");
chdir ("jobname");
create_pbs_script($folder,'V1','jobname');
system("qsub jobname_$folder\_V1.pbs");
chdir ("../../V2/");
mkdir ("jobname");
chdir ("jobname");
create_pbs_script($folder,'V2','jobname');
system("qsub jobname_$folder\_V2.pbs");
print "Leaving current directory\n";
chdir ($original_path);
}

################################################## ################

# Function to write PBS scripts
# test - print numbers 1 to 100

sub create_pbs_script {
my $folder = $_[0];
my ($vid, $chdid) = split /_/, $folder;
my $v_no = $_[1];
my $job = $_[2];
my $filename = "$job\_$folder\_$v_no.pbs";
#print $filename;
open(my $fh, '>', "$job\_$folder\_$v_no.pbs") or die " Cannot write PBS script $job\_$folder\_$v_no\n";
print $fh "#PBS -N $job\_$folder\_$v_no\n#PBS -q $queue_name\n#PBS -l nodes=1:ppn=1\n#PBS -l walltime=00:10:00\n#PBS -l mem=1gb\n#PBS -j oe\n\ncd \$PBS_O_WORKDIR\n\nfor i in {1..100};do echo \$i;done";
close $fh;
}

################################################## ################
Update : I inserted a system command to open and save in vim before submitting through qsub and the job gets executed. I have got this code to work, but I still don't know why I need to do this.
system("vim pbsfilename -c 'wq'");


Check Solution